2014-03-25 52 views
2

您好我正在開發一個android應用程序,數據庫是本地主機使用的數據庫的確切副本,也就是說在android數據庫和php數據庫中使用的字段數是相同。我在php數據庫中創建的任何條目都應該同步到android數據庫。我正在編寫下面的代碼,用於php側和android側,請告訴我爲什麼無法連接。無法連接我的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 
    } 


} 

我得到這says-連接到"http://ip_of_localhost/final_year_receive_request.php"拒絕

+0

在你的代碼,請避免標籤,更好的方法是使用4個空格進行縮進 – baldrs

回答

2

錯誤,您應該使用10.0.2.2代替127.0.0.1

另請注意,開發機器 上的地址127.0.0.1對應於仿真器自己的回送接口。如果您想要在您的開發機器的環回 接口(您的機器上的a.k.a. 127.0.0.1)上運行訪問服務 ,則應該使用 專用地址10.0.2.2。

Source

+0

更多信息> http://developer.android.com/tools/devices/emulator.html#networkaddresses – Svetoslav

+1

@ Shankar我正在創建一個服務器和應用程序的adhoc網絡。我得到了服務器的IP地址,並將該地址用作「ip_of_localhost」,我調用了receive_request.php文件,但它仍然不起作用。 – user8594

+0

@Svetlio,的確,我正要粘貼該鏈接;) –

相關問題