2011-12-17 106 views
5

我目前正在嘗試開發一個應用程序,其中包括可以發送和接收來自MySQL服務器的數據。從MySQL獲取數據到Android與PHP

該應用程序調用一個php腳本,它使連接到mysql服務器。我已經成功開發了發送部分,現在我想從mysql中檢索數據並將其顯示在android手機上。

MySQL表由5列組成:

  • bssid
  • building
  • floor
  • lon
  • lat

PHP文件getdata.php包含:

 <?php 
    $con = mysql_connect("localhost","root","xxx"); 
    if(!$con) 
    { 
     echo 'Not connected'; 
     echo ' - '; 

    }else 
    { 
    echo 'Connection Established'; 
    echo ' - '; 
    } 

    $db = mysql_select_db("android"); 
    if(!$db) 
    { 
     echo 'No database selected'; 
    }else 
    { 
     echo 'Database selected'; 
    } 
    $sql = mysql_query("SELECT building,floor,lon,lat FROM ap_location WHERE bssid='00:19:07:8e:f7:b0'"); 

    while($row=mysql_fetch_assoc($sql)) 
    $output[]=$row; 
    print(json_encode($output)); 

     mysql_close(); ?> 

這部分工作正常,在瀏覽器中測試時。

用於連接到PHP中的Java代碼:

public class Database { 
     public static Object[] getData(){ 
    String db_url = "http://xx.xx.xx.xx/getdata.php"; 
    InputStream is = null; 
    String line = null; 
    ArrayList<NameValuePair> request = new ArrayList<NameValuePair>(); 
    request.add(new BasicNameValuePair("bssid",bssid)); 
    Object returnValue[] = new Object[4]; 
    try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httppost = new HttpPost(db_url); 
     httppost.setEntity(new UrlEncodedFormEntity(request)); 
     HttpResponse response = httpclient.execute(httppost, localContext); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection" +e.toString()); 
    } 
    String result = ""; 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 

     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection" +e.toString()); 
    } 
    try 
    { 
     JSONArray jArray = new JSONArray(result); 
     JSONObject json_data = jArray.getJSONObject(0); 
     returnValue[0] = (json_data.getString("building")); 
     returnValue[1] = (json_data.getString("floor")); 
     returnValue[2] = (json_data.getString("lon")); 
     returnValue[3] = (json_data.getString("lat")); 

    }catch(JSONException e){ 
     Log.e("log_tag", "Error parsing data" +e.toString()); 
    } 
    return returnValue; 
} 

}

這是用於將數據發送到MySQL服務器,修改代碼,但什麼是錯的。 我試圖通過在代碼中設置不同的returnValue來測試它,這說明帶有httpclient連接的部件不運行。

你們能幫我嗎?

我希望這不是太困擾,如果你想,我可以試着解釋一下。

+0

BUMP測試。我需要進一步解釋我的問題嗎? – freddy 2011-12-20 23:44:06

+0

我測試了代碼,它適用於我。 – 2011-12-22 02:02:57

+0

您是否已將此權限添加到清單文件中? '<使用權限android:name =「android.permission.INTERNET」/>''' – Hovo 2014-12-19 02:28:50

回答

1

使用HttpGet代替HttpPost並解析你的url。

0

這是一類,我總是用得到

public JSONObject get(String urlString){  
    URL currentUrl; 
    try { 
     currentUrl = new URL(currentUrlString); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     return null; 
    } 

    HttpURLConnection urlConnection = null; 
    InputStream in; 
    BufferedReader streamReader = null; 
    StringBuilder responseStrBuilder = new StringBuilder(); 
    String inputStr; 
    try { 
     urlConnection = (HttpURLConnection) currentUrl.openConnection();    
     in = new BufferedInputStream(urlConnection.getInputStream()); 
     streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); 
     while ((inputStr = streamReader.readLine()) != null) { 
      responseStrBuilder.append(inputStr); 
     } 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     urlConnection.disconnect(); 
     if(null != streamReader){ 
      try { 
       streamReader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    try { 
     return new JSONObject(responseStrBuilder.toString()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

嘗試get("http://echo.jsontest.com/key/value");