Php android studio中的localhost被拒绝

Php android studio中的localhost被拒绝,php,android,json,wampserver,Php,Android,Json,Wampserver,我正在使用wampserver,并尝试通过json解析使用php脚本从服务器获取记录。我想在我的android应用程序上显示这些数据 我的脚本代码很清晰,而且工作得很完美。我对android代码也很清楚。但当我运行我的应用程序时,数据不会显示在我的应用程序上 这是我的android代码…… package com.example.abc.testdatabase; import java.io.BufferedReader; import java.io.InputStream; import

我正在使用wampserver,并尝试通过json解析使用php脚本从服务器获取记录。我想在我的android应用程序上显示这些数据

我的脚本代码很清晰,而且工作得很完美。我对android代码也很清楚。但当我运行我的应用程序时,数据不会显示在我的应用程序上

这是我的android代码……

package com.example.abc.testdatabase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;


public class TestExternalDatabaseActivity extends Activity {
/** Called when the activity is first created. */

TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   StrictMode.enableDefaults(); //STRICT MODE ENABLED
   resultView = (TextView) findViewById(R.id.result);

    getData();
}

public void getData(){
    String result = "";
    InputStream isr = null;
    try{
        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost("http://10.0.2.2/getAllCustomers.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
}
catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        resultView.setText("Couldnt connect to database");
}
//convert response to string
try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        isr.close();

        result=sb.toString();
}
catch(Exception e){
        Log.e("log_tag", "Error  converting result "+e.toString());
}

//parse json data
       try {
       String s = "";
       JSONArray jArray = new JSONArray(result);

   for(int i=0; i<jArray.length();i++){
       JSONObject json = jArray.getJSONObject(i);
       s = s + 
               "Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"\n"+
               "Qualification : "+json.getInt("Qualification")+"\n"+
               "Mobile Using : "+json.getString("Mobile")+"\n\n";
       }

      resultView.setText(s);

   } catch (Exception e) {
// TODO: handle exception
   Log.e("log_tag", "Error Parsing Data "+e.toString());
   }

    }

  }
Error Parsing Data org.json.JSONException: Value <br of type    java.lang.String cannot be converted to JSONArray
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
      die('Could not connect: ' . mysql_error()); 

}
 mysql_select_db("testDatabase", $con);
 $result = mysql_query("SELECT * FROM Customer");
 while($row = mysql_fetch_assoc($result))
 {
       $output[]=$row;
 }
 print(json_encode($output));
 mysql_close($con);

?>
package com.example.abc.testdatabase;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.widget.TextView;
公共类TesterExternalDatabaseActivity扩展活动{
/**在首次创建活动时调用*/
文本视图结果视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.enableDefaults();//已启用严格模式
resultView=(TextView)findViewById(R.id.result);
getData();
}
public void getData(){
字符串结果=”;
InputStream isr=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/getAllCustomers.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
isr=entity.getContent();
}
捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
resultView.setText(“无法连接到数据库”);
}
//将响应转换为字符串
试一试{
BufferedReader读取器=新的BufferedReader(新的InputStreamReader(isr,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
isr.close();
结果=sb.toString();
}
捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
字符串s=“”;
JSONArray jArray=新JSONArray(结果);

对于(int i=0;i检查
您从PHP文件收到的响应超过了JSON字符串。从我看到的情况来看,有一个额外的PHP警告声明“不推荐:mysql\u connect()…”除JSON字符串外,还将返回其他字符串。首先注意PHP端的警告,然后检查是否只返回纯JSON字符串。我已经编辑了PHP脚本,它工作正常,但我在android studio中仍然遇到了相同的错误。您是否检查了
result
变量的值?它会告诉您你怎么了……是的,我已经检查过了,它给了我整个html代码……但我不明白为什么……什么
整个html代码
?是异常吗?是异常吗?是错误消息吗?消息是什么?@VandanaRao更新了答案。
Log.e................
     <br />
     <font size='1'><table class='xdebug-error xe-deprecated' dir='ltr'            border='1' cellspacing='0' cellpadding='1'>
     <tr><th align='left' bgcolor='#f57900' colspan="5"><span   style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )   </span> Deprecated: mysql_connect(): The mysql extension is deprecated and will   be removed in the future: use mysqli or PDO instead in  C:\wamp\www\TestDatabase\connection.php on line <i>3</i></th></tr>
     <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
     <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
     <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0170</td><td bgcolor='#eeeeec' align='right'>135864</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>0</td></tr>
     <tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137344</td><td bgcolor='#eeeeec'>include_once( <font color='#00bb00'>'C:\wamp\www\TestDatabase\connection.php'</font> )</td><td title='C:\wamp\www\TestDatabase\getAllCustomer.php' bgcolor='#eeeeec'>..\getAllCustomer.php<b>:</b>4</td></tr>
     <tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0190</td><td bgcolor='#eeeeec' align='right'>137528</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysql-connect' target='_new'>mysql_connect</a>
(  )   </td><td title='C:\wamp\www\TestDatabase\connection.php' bgcolor='#eeeeec'>..\connection.php<b>:</b>3</td></tr>
       </table></font>
      [{"FirstName":"Vandana    ","LastName":"Rao","Qualification":"MscICT","Mobile":"Sony Xepri"}]
      03-23 10:29:26.965    2426-2426/com.example.abc.testdatabase E/log_tag﹕   Error Parsing Data org.json.JSONException: Value <br of type java.lang.String   cannot be converted to JSONArray
die(json_encode(array('result' => false, 'error' => $error)));