【问题标题】:Unable to connect my android app to localhost server无法将我的 android 应用程序连接到 localhost 服务器
【发布时间】:2014-03-25 08:58:32
【问题描述】:

您好我正在开发一个android应用程序,数据库是localhost中使用的数据库的精确副本,也就是说android db和php db中使用的字段数是相同的。我在 php db 中所做的任何条目都应该同步到 android db。我正在为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" 被拒绝

【问题讨论】:

  • 代码中请避免使用制表符,缩进最好使用4个空格

标签: php android mysql


【解决方案1】:

您应该使用10.0.2.2 而不是127.0.0.1

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

Source

【讨论】:

  • @Shankar 我正在创建服务器和应用程序的临时网络。我得到了服务器的 IP 地址,并将该地址用作“ip_of_localhost”,我调用了 receive_request.php 文件,但它仍然无法正常工作。
  • @Svetlio,没错,我正要粘贴那个链接;)
  • 你试过在它前面添加http://吗?为什么要通过 IP 顺便说一句?那个IP没有主机名吗?
  • @Shankar 看到问题是我不知道应用程序如何自动检测服务器的 ip,所以我将它传递给调用receicve_request.php 页面的函数,但是按下同步按钮我收到与此 IP 地址的连接被拒绝的相同错误,您认为它为什么会被拒绝?请帮帮我
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
相关资源
最近更新 更多