2013-05-08 176 views
0

我想從使用xampp mysql創建的localhost數據庫中檢索數據。Url連接android到mysql數據庫

public class JASONUseActivity extends Activity { 

    EditText byear; // To take birthyear as input from user 
    Button submit; 
    TextView tv; // TextView to show the result of MySQL query 

    String returnString; // to store the result of MySQL query after decoding JSON 

    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread 
     .penaltyLog().build()); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     byear = (EditText) findViewById(R.id.editText1); 
     submit = (Button) findViewById(R.id.submitbutton); 
     tv = (TextView) findViewById(R.id.showresult); 

     // define the action when user clicks on submit button 
     submit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       // declare parameters that are passed to PHP script i.e. the name birth year and its value submitted by user 
       ArrayList <NameValuePair> postParameters = new ArrayList <NameValuePair>(); 

       // define the parameter 
       postParameters.add(new BasicNameValuePair("birthyear", byear.getText().toString())); 
       String response = null; 

       // call executeHttpPost method passing necessary parameters 
       try { 
        response = CustomHttpClient.executeHttpPost("http://localhost/jasonscript.php", postParameters); 

        // store the result returned by PHP script that runs MySQL query 
        String result = response.toString(); 

我無法在textview中查看數據。網址是否正確?

這裏是位於XAMPP的jasonscript.php/htdocs目錄

<?php 
$db_host = "localhost"; 

$db_uid = "root"; 

$db_pass = ""; 

$db_name = ""; 

$db_con = mysql_connect($db_host, $db_uid, $db_pass) or die('could not connect'); 
mysql_select_db($db_name); 
$sql = "SELECT * FROM people WHERE birthyear > '" . $_POST["birthyear"] . "'"; 
$result = mysql_query($sql); 
while ($row = mysql_fetch_assoc($result)) 
    $output[] = $row; 
print(json_encode($output)); 
mysql_close(); 
?> 

我已經給網絡許可清單文件。

在此先感謝。

+0

我認爲Android沒有錯誤?你能否確認(可能你沒有得到強制關閉,因爲你有一個try/catch)。如果Anrdoi沒有錯誤 - 檢查你的服務器是否出錯 - 右鍵點擊並轉到apache - 錯誤日誌。如果有任何錯誤,請運行您的應用程序,並從當時的日誌中獲取最後一行。 – 2013-05-08 06:38:14

+0

連接電腦的IP不是本地主機 – 2013-05-08 06:46:00

+0

沒有該應用程序不強制關閉。在Apache錯誤日誌中沒有錯誤。我在logcat中遇到了嚴格的模式錯誤。 – 2013-05-08 08:53:31

回答

0

當您在Android設備上使用localhost時,您正在嘗試設備本身的服務。使用服務器的IP /主機名來獲取數據。

旁註:也看看到ASyncTaskhere因爲的onCreate下載數據會鎖定您的UI線程(如果需要很長的),可以導致App Not RespondingForce Close。即使它不會導致崩潰,用戶體驗也會因應用程序不響應而降低。

+0

所以,我應該給「ipaddress/jasonscript.php或」ipaddress/localhost/jasonscript.php? – 2013-05-08 08:55:55

+0

'CustomHttpClient.executeHttpPost(「http://api.mywebserver.com/jasonscript.php」,postParameters);'OR'CustomHttpClient.executeHttpPost(「http://192.168.0.2/jasonscript.php」,postParameters);'當然你自己的IP地址 – 2013-05-08 10:08:28

0

localhost在設備上引用其回送接口,而不是您的開發計算機託管您正在嘗試連接的數據庫。爲了讓虛擬設備連接到開發機器的環回接口,您需要使用10.0.2.2。你可以將網址變更到像這樣打你的開發機器:

CustomHttpClient.executeHttpPost("http://10.0.2.2/jasonscript.php", postParameters); 

見我的回答here瞭解有關仿真器的網絡如何運作的更多細節。