Android Volley - StringRequest 无响应
Android Volley - No Response from StringRequest
我在使用 Android Volley 时无法获得 JSON
响应。没有错误也没有成功响应,请参阅 (Log.d("logr=",_response);
)。我使用 StringRequest 从 Google Map API Android V2 中获取 JSON 文本。这是代码
private String urla = "https://maps.googleapis.com/maps/api/place/search/json?location=";
private String urlb = "&types=hotel&radius=500&sensor=false&key=YOUR_KEY";
@Override //::LocationListener (Interface)
public void onLocationChanged(Location location) {
//Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), 13);
googleMap.animateCamera(cameraUpdate);
double x = location.getLatitude();
double y = location.getLongitude();
String request = urla+x+","+y+urlb;
Log.d("log1=",request);
StringRequest stringRequest = new StringRequest(Request.Method.GET, request,
new Response.Listener<String>() {
@Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
Toast.makeText(activity, "Update location user ==>" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "Current Location :" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude());
}
这个问题有什么解决办法吗?
你实际上在哪里执行请求?你必须 add()
它到 Volley 的 RequestQueue
,否则请求根本不会被发送。
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = urla + x + "," + y + urlb;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
//excecute your request
queue.add(stringRequest);
在此处查看更多信息:Learn volley
试试这个:
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
final StringRequest stringRequest = new StringRequest(Request.Method.POST, urlString, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
strr = response;
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
Log.e("Volley", "ERROR");
}
});
requestQueue.add(stringRequest);
我在使用 Android Volley 时无法获得 JSON
响应。没有错误也没有成功响应,请参阅 (Log.d("logr=",_response);
)。我使用 StringRequest 从 Google Map API Android V2 中获取 JSON 文本。这是代码
private String urla = "https://maps.googleapis.com/maps/api/place/search/json?location=";
private String urlb = "&types=hotel&radius=500&sensor=false&key=YOUR_KEY";
@Override //::LocationListener (Interface)
public void onLocationChanged(Location location) {
//Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()), 13);
googleMap.animateCamera(cameraUpdate);
double x = location.getLatitude();
double y = location.getLongitude();
String request = urla+x+","+y+urlb;
Log.d("log1=",request);
StringRequest stringRequest = new StringRequest(Request.Method.GET, request,
new Response.Listener<String>() {
@Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
Toast.makeText(activity, "Update location user ==>" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "Current Location :" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude());
}
这个问题有什么解决办法吗?
你实际上在哪里执行请求?你必须 add()
它到 Volley 的 RequestQueue
,否则请求根本不会被发送。
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = urla + x + "," + y + urlb;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String _response) {
Log.d("logr=",_response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error handling
Log.d("log2=", error.toString());
Log.e("log3=", error.toString());
}
});
//excecute your request
queue.add(stringRequest);
在此处查看更多信息:Learn volley
试试这个:
requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
final StringRequest stringRequest = new StringRequest(Request.Method.POST, urlString, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
strr = response;
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
Log.e("Volley", "ERROR");
}
});
requestQueue.add(stringRequest);