我有两个凌空请求值从第二个请求返回第一个请求,其中值用于制作循环视图。返回值,我将参数作为字段,但其值不会从0更改为默认值。
我的第一次截击请求:
private void callplcaedetails(String id) {
RequestQueue queue = Volley.newRequestQueue(this);
System.out.println("id" + id);
String url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + id + "&key=AIzaSyDi9vrZ0F3TX***********Q";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
// Log.i("Response is: ", response);
JSONObject m = null;
try {
m = new JSONObject(response);
reader1 = m.getJSONObject("result");
String nameofplace = reader1.getString("name");
JSONObject geometry = reader1.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
String lat = location.getString("lat");
String lng = location.getString("lng");
Log.i("lat" + lat, "lng" + lng);
Addressname = reader1.getString("formatted_address");
float x1=getdistance(lat,lng);
Log.i("disatancccccccc",x1+"");
placelist.add(new Place(nameofplace, Addressname, x1));
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Not worked", "That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
我的第二次截击请求:
private Float getdistance(String lat, String lng) {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins="+latitude+","+longitude+"&destinations="+lat+","+lng+"&key=AIzaSyDi9vrZ0F3T******Y3GlQ";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.i("Response is: ", response);
JSONObject m = null;
try {
m = new JSONObject(response);
JSONArray reader1 = m.getJSONArray("rows");
JSONObject e = reader1.getJSONObject(0);
JSONArray f = e.getJSONArray("elements");
JSONObject g =f.getJSONObject(0);
JSONObject h= g.getJSONObject("distance");
String dist = h.getString("value");
x = Float.parseFloat(dist);
Log.i("distance",x+"");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Not worked", "That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
System.out.println("Distance Value"+x);
return x;
}
日志响应
I/disatancccccccc: 0.0
I/lat22.9575252: lng76.04197789999999
I/System.out: dkkvnjnvfnvjnvdfjvndfvjn0.0
I/disatancccccccc: 0.0
I/Response is:: "Response works right"
I/distance: 3595.0
距离正确,这有什么问题,哪种方式更好。
答案 0 :(得分:0)
注意: i made the parameter as final, but its value does not change from 0 which was default.
在宣布variable final
时,请在您的问题中提及,这意味着在将其声明为cant change
后,您final
了其值你可以从最终的名字中理解。
<强>解决方案:强>
您必须将变量声明为class level
,而dont
将变量声明为final
并将值赋给它然后再使用它。
public class AboutUsActivity extends Activity {
int x=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
//your response callback value
void XYZResponce(int result){
x = result;
}
}