我在慢速网络上遇到Volley GET请求问题。每当我在LogCat中看到BasicNetwork.logSlowRequests时,我的GET请求会被执行两次或更多次,从而导致1次请求的多个(2次或更多次)发布。我已经设置了重试政策,但它没有帮助。
这是我的LogCat
03-16 01:31:35.674: D/Volley(5984): [19807] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1> [lifetime=3824], [size=313], [rc=200], [retryCount=0] 03-16 01:31:35.704: D/Volley(5984): [1] Request.finish: 3853 ms: [ ] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1
StringRequest strReq = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pd.dismiss();
getColorDetails.getColorresponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
showSignOutAlertDialog(context, "TimeoutError");
} else if (error instanceof AuthFailureError) {
showSignOutAlertDialog(context, "AuthFailureError");
} else if (error instanceof ServerError) {
showSignOutAlertDialog(context, "ServerError");
} else if (error instanceof NetworkError) {
showSignOutAlertDialog(context, "NetworkError");
} else if (error instanceof ParseError) {
showSignOutAlertDialog(context, "ParseError");
}
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
int mStatusCode = response.statusCode;
System.out.println("Status code is===>"+mStatusCode);
return super.parseNetworkResponse(response);
}
};
strReq.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(strReq);
}