2010-11-24 54 views
1

我有一個應用程序從遠程數據庫獲取一些數據。 我使用PHP與下面的代碼連接到數據庫。安卓mysql php問題

mysql_connect($host,$username,$password) or die("no connection"); 
@mysql_select_db($database) or die("Unable to select database"); 

$query = $_REQUEST['query']; 
$q=mysql_query($query); 

while($e=mysql_fetch_assoc($q)) { 
    $output[]=$e; 
} 
print(json_encode($output)); 

mysql_close(); 

我然後通過下面的Java代碼連接

public void connect(ArrayList<NameValuePair> nameValuePairs) { 
     result = ""; 
     InputStream is = null; 

     String url = "http://'ipadress'/PhpProject1/EmptyPHP.php"; 

     //Get the content 
     try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 

      is = httpEntity.getContent(); 
     } catch (Exception e) { 
      Log.e("Connect", "Error in http connection " + e.toString()); 
     } 
     //Convert content toString 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
      //result = replaceString(sb.toString()); 
     } catch (Exception e) { 
      Log.e("Connect", "Error converting result " + e.toString()); 
     } 
    } 

當我已經做到了我做一個查詢通過

public void query(String query){ 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("query", query)); 
     connect(nameValuePairs); 
    } 

雖然這與模擬器的偉大工程有當問題在手機上使用它。

任何人都知道這是爲什麼?

預先感謝您

+0

請描述您遇到的問題。 – 2010-11-24 19:58:28

回答

0

確保你真正的設備連接到專用網絡居然能訪問該服務器。

最簡單的選擇將是與服務器在同一子網中的WiFi網絡。否則,您的手機將無法訪問網絡,因爲它不公開。