【发布时间】:2015-12-18 05:35:10
【问题描述】:
我正在尝试从我的数据库中返回多行。我有两个要检索的坐标,它们存储在我的数据库中,即经度和纬度。由于我的 asynctask 目前只能返回一行。我应该如何更改我的代码以便它能够从数据库中返回多行?
提前感谢您的帮助。
以下是我的代码:
php 代码
$user=$_POST["username"];
$query = "SELECT longitude,latitude FROM friends INNER JOIN coordinates ON friends.username = coordinates.username WHERE friends.friend_of='$user'";
$sql=mysqli_query($conn, $query);
if (!$sql) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysqli_fetch_array($sql)>0){
$response["longitude"] = $row[0];
$response["latitude"] = $row[1];
die(json_encode($response));
}
Asynctask 类。
class Findfriends extends AsyncTask<String, String, JSONObject> {
protected JSONObject doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
try {
HashMap<String, String> params = new HashMap<>();
params.put("username", args[0]);
JSONObject json = jsonParser.makeHttpRequest(
GET_FRIENDS, "POST", params);
if (json != null) {
Log.d("JSON result", json.toString());
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(JSONObject json) {
if (json != null) {
Toast.makeText(Borrower_AP.this, json.toString(),
Toast.LENGTH_LONG).show();
try {
Longitude = json.getDouble("longitude");
Latitude = json.getDouble("latitude");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
【问题讨论】:
-
只返回一个你想要返回的元素列表。
-
只需创建一个模型来保存您想要返回的数据。
-
您可以返回 jsonarray 或返回行列表
-
@KishuDroid 有没有例子说明如何做?
-
@IngoSchwarz 有什么进一步的解释吗?
标签: android mysql android-asynctask