本地主机在Android Studio中拒绝


localhost refused in android studio

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

我的脚本代码很清楚,而且效果很好。而且我也很清楚安卓代码。但是当我运行我的应用程序时,数据不会显示在我的应用程序上。

这是我的安卓代码。

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脚本.....

<?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);
?>

我不明白为什么我没有得到任何记录....如果有人绕过这个,那么请帮忙.....

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

异常的<br部分表示您得到的响应不是 json 响应,而是 html。
我猜你的结果是一个例外(或die('Could not connect: ' . mysql_error());的输出(。

在尝试解析result字符串之前记录它,我相信您会找到有关该错误的更多信息。


错误消息添加到原始帖子后更新:

您的错误消息是有关已弃用函数(mysql_* api(的通知,该函数是不应使用的 API。
您收到此通知是因为您显示所有错误(这在开发中很好(。
我建议切换到错误消息所说的mysqliPDO

我建议将 php 脚本捕获的任何错误消息作为失败响应返回,并带有 json 中的错误消息,例如:

die(json_encode(array('result' => false, 'error' => $error)));

这样,您不仅可以将它们打印为 html 或文本,还可以在 java 代码中捕获它们,并根据需要在该侧处理它们。
您还应该在 php 脚本中将内容类型设置为application/json,但即使没有它,它也应该可以工作。