我试图从MySQL数据库中获取数据,该数据库位于web服务器上。我的目标是检索已经在MySQL数据库上的位置数据数组。我从服务器获取这个数据数组,但我不能将它从jsonResponse.getJSONArray转换为JSONArray对象。
private void setUpMap() {
Response.Listener<String> responseListener=new Response.Listener<String>(){
@Override
public void onResponse(String s) {
try {
JSONObject jsonResponse=new JSONObject(s);
boolean success=jsonResponse.getBoolean("success");
if(success){ //this is true, and also all the data is arrived
JSONArray jsonArray = jsonResponse.getJSONArray("data"); //after this line it doesnt work,it doesn't enter in for loop
JSONObject jsonObject;
for(int i=0;i<jsonArray.length();i++){
jsonObject=jsonArray.getJSONObject(i);
String mac=jsonObject.getString("mac");
String android_id=jsonObject.getString("android_id");
Double latitude=jsonObject.getDouble("latitude");
Double longitude=jsonObject.getDouble("longitude");
if(!isMarkerOnArray(markerCollection,latitude,longitude))
markerCollection.add(mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))));
}
}
else{
AlertDialog.Builder builder=new AlertDialog.Builder(MapsActivity.this);
builder.setMessage("Downloading position failed")
.setNegativeButton("retry",null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
DownloadPosition downloadPosition=new DownloadPosition(responseListener);
RequestQueue queue= Volley.newRequestQueue(MapsActivity.this);
queue.add(downloadPosition);
}调试
这是我的php脚本
`<?php
$con=mysqli_connect("","","","");
$sql="SELECT * FROM positions";
$result=mysqli_query($con,$sql);
$response["data"]=array();
$data=array();
$data["success"]=false;
$i=0;
while($rs = mysqli_fetch_assoc($result)){
$data["success"]=true;
$data[$i] = array('id'=>$rs['id'],'mac'=>$rs['mac'], 'android_id'=>$rs['android_id'], 'latitude'=> $rs['latitude'],'longitude'=> $rs['longitude']);
$i++;
}
echo json_encode($data);
mysqli_close($con);
?>`因此,我的问题是如何从JSONArray ()获取jsonResponse.getJSONArray对象?
编辑: 我是杰森
发布于 2016-07-02 00:27:30
您将得到响应中的JsonObject,其中包含布尔值和JsonObject。
所以,修改PHP代码,
$result=array();
while($rs = mysqli_fetch_assoc($result)){
$result[$i] = array('id'=>$rs['id'],'mac'=>$rs['mac'], 'android_id'=>$rs['android_id'], 'latitude'=> $rs['latitude'],'longitude'=> $rs['longitude']);
$i++;
}
$data['data']=$result;
echo json_encode($data);https://stackoverflow.com/questions/38155184
复制相似问题