无法连接我的安卓应用程序到本地主机服务器


Unable to connect my android app to localhost server

嗨,我正在开发一个android应用程序,数据库是本地主机中使用的数据库的精确副本,也就是说android db和php db中使用的字段数是相同的。我在php数据库中的任何条目都应该同步到android数据库。我正在为php端和android端编写下面的代码,请告诉我为什么无法连接。

receive_reques.php

<?php
// receive_request.php
// get the last entry from the database table
$query = "select last(Bank_Name,Account,Type,Date,Amount,Cheque_no,Cheque_party,Cheque_details) from `transaction`";
$query_run = mysql_query($query);
$response = "";
$response = mysql_fetch_row($query_run);
echo implode(":",$response);
?>
Android同步代码:
public void postData()
{
    HttpClient httpclient  = new DefaultHttpClient();

    //  calls the local host and then the receive_request file for data values
    HttpPost httppost = new HttpPost("ip_of_localhost'final_year'receive_request.php");
    try{
        HttpResponse response = httpclient.execute(httppost);
        // convert the response received to String format
        String array[] = EntityUtils.toString(response.getEntity()).split(":");
        // now store them in dbms and display them in the layout
    } catch (ClientProtocolException e) {
        // process execption
    } catch (IOException e) {
        // process execption
    }

}

我得到一个错误,说-连接到"http://ip_of_localhost/final_year_receive_request.php"拒绝

您应该使用10.0.2.2而不是127.0.0.1

还要注意开发机器上的地址127.0.0.1对应于模拟器自己的环回接口。如果你想访问在开发机器的环回上运行的服务接口(在您的机器上称为127.0.0.1),您应该使用特殊地址10.0.2.2代替

Source