2016-10-03 7 views
0

からデータを取得し得る私は私のデータの符号化JSONは、私が唯一取得したいこののAndroid Studioは、データベースからのTextView

{"tbl_accepted":[{"renterLat":"15.706376","renterLng":"121.065852"}]} 

のフォーマットは、緯度とLNGの値ではありませんすることができますので、私それに応じて別々のtextViewsでそれらを表示することができます。そして、私はそれをどうやって得るのですか?事前拝啓で

private void getJSON(String url){ 
    class GetJSON extends AsyncTask<String, Void, String>{ 
     protected String doInBackground(String... params){ 
      String uri = params[0]; 
      BufferedReader bufferedReader = null; 
      try{ 

       URL url = new URL(uri); 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
       StringBuilder sb = new StringBuilder(); 
       bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

       String json; 
       while((json=bufferedReader.readLine()) != null){ 
        sb.append(json+"\n"); 
       } 
       return sb.toString().trim(); 
      }catch(Exception e){ 
       return null; 
      } 
     } 

     @Override 
     protected void onPostExecute(String s){ 
      super.onPostExecute(s); 
      tvLat.setText(s); 
      Log.d(TAG,tvLat.getText().toString()); 
     } 
    } 
    GetJSON gj = new GetJSON(); 
    gj.execute(url); 
} 

PHP

<?php 
include_once("connection.php"); 
$where = ''; 
if (isset($_GET['renter'])){ 
$where = " WHERE renter like '%".addslashes($_GET['renter'])."%'"; 
} 
$sql = "SELECT renterLat, renterLng FROM tbl_accepted ".$where." ORDER BY acceptedID DESC"; 

$result = mysqli_query($conn, $sql); 
$json = array(); 

if(mysqli_num_rows($result)){ 

while($row = mysqli_fetch_assoc($result)){ 

    $json['tbl_accepted'][]=$row; 
} 
} 

mysqli_close($conn); 
echo json_encode($json); 

?> 

感謝。

+0

@Selvin - 私はその質問の答えとして記載された 'org.json'について学ぶためにしようとするでしょう:) –

答えて

0

次の文字列は、結果としてonPostExecuteになります。 オブジェクトJSON文字列を変換する

{"tbl_accepted":[{"renterLat":"15.706376","renterLng":"121.065852"}]} 

使用JSONObjectJSONArray

文字列をオブジェクトに変換すると、onPostExecuteのようになります。

@Override 
     protected void onPostExecute(String s){ 
      super.onPostExecute(s); 
      JSONObject latObj=new JSONObject(s); 
      JSONArray tbl_accepted=latObj.getJSONArray('tbl_accepted'); 
      tvLat.setText(tbl_accepted.getJSONObject(0).getString('renterLat')); 
      Log.d(TAG,tvLat.getText().toString()); 
     } 
+0

tvLat.setText'上(tbl_accepted [0] .getString( "renterLat" )); 'tbl_acceptedは' Array type expected;のエラーを持っています。見つかった: 'org.json.JSONArray'' –

+0

残念ですが、代わりに 'getJSONObject'が必要です。更新されたコードを確認してください。 –

関連する問題