我使用服务器中的凌空获得了纬度和经度列表。这是功能。
private List<LatLng> getLocations() {
final List<LatLng> pos = new ArrayList<>();
StringRequest rqst = new StringRequest(Request.Method.POST, "http://www.findandgo.in/server/getAllUsersLocation.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i=0; i<array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Double lat = Double.parseDouble(object.getString("user_latitude"));
Double longi = Double.parseDouble(object.getString("user_longitude"));
LatLng latLng = new LatLng(lat,longi);
pos.add(latLng);
//here the list is returning size
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(HomeActivity.this,error.toString(),Toast.LENGTH_SHORT).show();
}
});
RequestQueue rq = Volley.newRequestQueue(getApplicationContext());
rq.add(rqst);
//here the list is showing size 0
return pos;
}
上面的函数是返回的空列表,但是当我在onResponse中使用日志值以查看是否存在该值时,它显示了。
的原因是,在射击课之外,您将永远不会得到结果。因为它是异步的。使用回电功能获取结果或在该凌空方法中使用代码
private void getLocations() {
final List<LatLng> pos = new ArrayList<>();
StringRequest rqst = new StringRequest(Request.Method.POST, "http://www.findandgo.in/server/getAllUsersLocation.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i=0; i<array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Double lat = Double.parseDouble(object.getString("user_latitude"));
Double longi = Double.parseDouble(object.getString("user_longitude"));
LatLng latLng = new LatLng(lat,longi);
pos.add(latLng);
}
//do your code here
SomthingFunction(pos); call a function
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(HomeActivity.this,error.toString(),Toast.LENGTH_SHORT).show();
}
});
RequestQueue rq = Volley.newRequestQueue(getApplicationContext());
rq.add(rqst);
}
齐射本质上是异步的,因此期望此处的顺序流不正确。查看文档所说的内容 -
强订单使您易于正确填充您的UI 从网络中获取数据异步。
成功发布后客户端接收服务器响应时,请调用onResponse((方法。
当您返回列表" pos"(在方法主体的末尾(时,它是空的,因为当时服务器尚未收到次要值的列表。
因此,您必须在OnResponse((中返回列表,以便在接收服务器响应时将填充的列表返回到呼叫者函数。
try {
JSONArray array = new JSONArray(response);
for (int i=0; i<array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Double lat = Double.parseDouble(object.getString("user_latitude"));
Double longi = Double.parseDouble(object.getString("user_longitude"));
LatLng latLng = new LatLng(lat,longi);
pos.add(latLng);
//here the list is returning size
}
return pos; //return list here (since list is populated now)
} catch (JSONException e) {
e.printStackTrace();
}
这并不是最好的方法,因为服务器可能需要时间发送响应。因此,请确保您的UI相应地处理此延迟(同时显示progressDialog(。
edit - 由于您无法从OnResponse返回列表&lt;>您可以使用此处所示的回调接口。
使用回调接口的示例代码 -
@Override
public void onResponse(String response) {
coordinates = parseLocation(response);
if((coordinates == null && coordinates.isEmpty())||coordinates.equals("NOT_VALID_ADDRESS")) {
showAlert("Please Enter A Valid Zip Code");
} else {
useCoordinates(coordinates);
}
}
Then implement the method that uses the coordinates:
public void useCoordinates(Location coordinates) {
// Use the value
}