2011-12-17 17 views
5

私は現在、他のものの中でも特にmysqlサーバからデータを送受信できるアプリを開発しようとしています。mysqlからアンドロイドへのデータをPHPで取得

アプリは、mysqlサーバとの接続を行うphpスクリプトを呼び出します。私は正常に送信部分を開発し、今私はmysqlからデータを取得し、アンドロイド携帯電話でそれを表示したい。

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

+0

私はコードをテストし、それは私のために正常に動作します。 –

+0

この権限をマニフェストファイルに追加しましたか? '' – Hovo

答えて

1

HttpPostの代わりにHttpGetを使用して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");

関連する問題