简体   繁体   English

android studio中的本地主机被拒绝

[英]localhost refused in android studio

I am using wampserver and trying fetch record from server using php script through json parsing. 我正在使用wampserver并尝试通过json解析使用php脚本从服务器获取记录。 And I want to display that data on my android app.... 我想在我的Android应用程序上显示该数据。

My script code is clear and it worked perfect..And I am also clear with android code. 我的脚本代码很清楚,并且运行得非常完美。而且我对Android代码也很清楚。 But when I run my app the data is not display on my app.... 但是当我运行我的应用程序时,数据不会显示在我的应用程序上。

This is my android code.... 这是我的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());
   }

    }

  }

Following is error I am facing........... 以下是我面临的错误...........

Error Parsing Data org.json.JSONException: Value <br of type    java.lang.String cannot be converted to JSONArray

Following is my php script........... 以下是我的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);

?>

I don't understand why I am not getting any records.... If anyone round this then please help for this..... 我不明白为什么我没有得到任何记录....如果有人对此进行了处理,请为此提供帮助.....

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

The <br part of the exception indicates that the response you get is not a json response, but rather html. 异常的<br部分表示您收到的响应不是json响应,而是html。
I would guess that your result is an exception (or the output from die('Could not connect: ' . mysql_error()); ). 我猜你的结果是一个异常(或die('Could not connect: ' . mysql_error()); )。

Log the result string before trying to parse it, and I'm sure you will find a lot more information about the error. 尝试解析result之前,请记录result字符串,我相信您会找到有关该错误的更多信息。


Update after error message was added to original post: 错误消息添加到原始帖子后更新:

Your error message is a notice about a deprecated function (the mysql_* api), an api that should not be used. 您的错误消息是关于不推荐使用的函数( mysql_* api)(不应该使用的api)的通知。
You get this notice cause you show all errors (Which is good in development). 您收到此通知是因为您显示了所有错误(这在开发中很有用)。
I would recommend switching to mysqli or PDO as the error message says. 我建议按照错误消息所述切换到mysqliPDO

I would recommend that any error messages caught by the php script is returned as a failure response with a error message in json, like: 我建议将php脚本捕获的任何错误消息作为失败响应返回,并在json中显示一条错误消息,例如:

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

Instead of just printing them as html or text, this way you can catch them in the java code and handle them as you wish on that side. 这样,您不仅可以将它们打印为html或文本,还可以将其捕获到Java代码中,并根据需要进行处理。
You should also set the content type to application/json in the php-script, but it should work even without that. 您还应该在php脚本中将内容类型设置为application/json ,但是即使没有该设置也可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM