我可以讓我的數據的編碼的JSON不是這個的Android Studio取得從數據庫中獲取數據,以TextView的
{"tbl_accepted":[{"renterLat":"15.706376","renterLng":"121.065852"}]}
我只想要得到的格式是緯度和LNG,所以我的價值觀可以相應地在單獨的textView中顯示它們。這裏是我如何得到它。
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);
?>
在此先感謝先生們。
@Selvin - 我會試着瞭解關於'tvLat.setText(tbl_accepted [0] .getString( 「renterLat」 的'org.json'這是說作爲這個問題的答案:) –