您应该设置请求的重试策略:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
这会将您的代码更改为:
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest request = new JsonObjectRequest(Method.GET, cityListUrl, null, new
Listener<JSONObject>() {
public void onResponse(JSONObject jsonResults) {
//Any Call
}
}, new ErrorListener(){
public void onErrorResponse(VolleyError arg0) {
//Any Error log
}
}
);
int socketTimeout = 30000;//30 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
mRequestQueue.add(request);
如果您刚刚开始使用Volley,则可能需要考虑使用droidQuery,它更易于配置:
int socketTimeout = 30000;
$.ajax(new AjaxOptions().url(cityListUrl)
.timeout(socketTimeout)
.success(new Function() {
public void invoke($ d, Object... args) {
JSONObject jsonResults = (JSONObject) args[0];
//Any call
}
})
.error(new Function() {
public void invoke($ d, Object... args) {
AjaxError error = (AjaxError) args[0];
Log.e("Ajax", error.toString());
}
}));