簡體   English   中英

無法使用php將android應用與mysql DB連接

[英]Unable to connect android app with mysql DB using php

以下是我的代碼,該代碼將android應用程序與MySQL DB連接。 當我在android手機中運行此程序時,它只會顯示一個純屏幕。 我的logcat中沒有錯誤。 當我使用"192.168.1.11/city.php"在瀏覽器中運行它時,我從DB中提取了該城市。 但是當我在android設備中運行它時,同樣的方法不起作用。 請幫忙

Android類:

 public class City extends ListActivity {

     JSONArray jArray;
     String result = null;
     InputStream is = null;
     StringBuilder sb=null;

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     //http post
     try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.1.11/city.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
        }
       //convert response to string
       try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line="0";
        while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
        }
    is.close();
    result=sb.toString();
    }catch(Exception e){
          Log.e("log_tag", "Error converting result "+e.toString());
    }
   //paring data
   int ct_id;
   String ct_name;
   try{
  jArray = new JSONArray(result);
  JSONObject json_data=null;
  for(int i=0;i<jArray.length();i++){
         json_data = jArray.getJSONObject(i);
         ct_id=json_data.getInt("CITY_ID");
         ct_name=json_data.getString("CITY_NAME");
     }
  }
  catch(JSONException e1){
      Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
  } catch (ParseException e1) {
        e1.printStackTrace();
}
   }
    }
}

activity_city.xml代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".City" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

郵遞區號:

 <?php
 ob_start();
 $host="localhost"; // Host name 
 $username=""; // Mysql username 
 $password=""; // Mysql password 
 $db_name="test"; // Database name 
 $tbl_name="CITY"; // Table name 
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");
 $sql=mysql_query("select * from CITY");
 while($row=mysql_fetch_assoc($sql))
{
$output[]=$row;
print(json_encode($output));
}
mysql_close();
?>

我的Android清單文件中包含INTERNET權限。

我不知道我要去哪里錯了。 請幫忙。

確保您的電話與服務器位於同一網絡中。

嘗試移動

print(json_encode($ output));

跳出循環

您的PHP代碼需要更改..因此,我將其更改並放回原處。

<?php
     ob_start();
     $host="localhost"; // Host name              Changed
     $username="root"; // Mysql username          Changed
     $password=""; // Mysql password 
     $db_name="test"; // Database name 

     $tbl_name="CITY"; // Table name 
     mysql_connect($host, $username, $password)or die("cannot connect");          //Changed
     mysql_select_db($db_name)or die("cannot select DB");           //Changed
     $sql=mysql_query("select * from CITY");
     while($row=mysql_fetch_assoc($sql))
     {
          $output[]=$row;
          print(json_encode($output));
     }
     mysql_close();
?>

PHP文件在哪里。 在本地計算機上,或在Web服務器上。

如果PHP文件位於本地計算機上,則無法從android應用程序訪問它。

還要確認您已為android應用程序授予了互聯網權限。

如果要在手機上測試應用程序,請不要使用localhost。 您的電話無法訪問localhost上的PHP文件。

可能我建議在線托管您的PHP和SQL文件並繼續測試。

1)您確定您的本地主機IP是“ http://192.168.1.11 ”嗎? 嘗試把兩個/這樣的“ http://192.168.1.11//city.php

我已經將此IP用於我的xampp“ http://10.0.2.2//Mobile/login2.php ”,或者也嘗試使用127.0.1.1或127.0.0.1

2)試試這個鏈接 如果您在Java或php代碼中做錯了什么,那將對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM