我正在尝试从数据库中返回多行。我有两个要检索的坐标存储在我的数据库中,即经度和纬度。因为我的asynctask目前只能返回一行。我应该如何更改代码,以便能够从数据库中返回多行?
感谢您的帮助。
以下是我的代码:
php code
$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();
}
}
}
}
答案 0 :(得分:0)
我认为你必须在这里犯错,第一个是你的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;
}
$rows = array();
while($r = mysqli_fetch_assoc($sql)) {
$rows[] = $r;
}
print json_encode($rows);
另一方面,在您的Android代码中,如果您使用的是Google Maps Api,则可以返回一个ArrayList,如下所示:
class Findfriends extends AsyncTask<String, String, ArrayList<LatLong> {
protected ArrayList<LatLong> doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
ArrayList<LatLong> geoPos=new ArrayList<LatLong>();
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());
//Complete here the code in which you iterate over the json and add each point to your ArrayList<LatLong>
}
} catch (Exception e) {
e.printStackTrace();
}
return geoPos;
}
}
答案 1 :(得分:0)
请改变这样的php脚本,
$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(mysqli_num_rows($sql)>0)
{
while($row = mysqli_fetch_array($sql)
{
$latlongArray =array();
$latlongArray ["longitude"] = $row[0];
$latlongArray ["latitude"] = $row[1];
array_push($response["DATA"],$latlongArray)
}
$response["success"] = "1";
echo json_encode($response);
}
else
{
$response["success"] = "0";
echo json_encode($response);
}
在android代码中,在Post执行粘贴此代码,在logcat中,您将看到数据库中的lat / long。
JSONArray ja = json.getJSONArray("DATA");
for (int r = 0; r < ja.length(); r++) {
JSONObject obj = ja.getJSONObject(r);
Log.i("Lat / Long",obj.getString("latitude")+" , "+obj.getString("longitude"));
}
试试这可能对你有所帮助..而且它正在运行代码。 谢谢,
答案 2 :(得分:0)
像这样创建自己的处理程序:
//import android.os.Handler; this import is required.
public class MyHandler extends Handler{
public void handleMessage(Message msg) {
super.handleMessage(msg);
List<YourObject> objs=(List<YourObject>)msg.obj;
}
}
在您的活动中创建异步对象,如下所示:
MyDataFetcher mdf=new MyDataFetcher(new MyHandler());
像这样写你的异步:
class MyDataFetcher extends AsyncTask<String, String, JSONObject> {
private Handler handler
public MyDataFetcher(Handler handler){
this.handler=handler;
}
//here is code you need to write in onPost
protected void onPostExecute(JSONObject json) {
if (json != null) {
Toast.makeText(Borrower_AP.this, json.toString(),
Toast.LENGTH_LONG).show();
try {
List<YourObject> objs=new ArrayList<>();
// populate your info from json into objs
//------your code here for above logic
//Now create new Message
Message msg=new Message();
msg.obj=objs;
handler.handleMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}