RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.add(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
}
}
));
This is my Request Call and i want to change or set timeout for the request . Is it possible anyway ??
To handle Volley timeouts you need to use a RetryPolicy. A retry policy is used in case a request cannot be completed due to network failure or some other cases. Volley provides an easy way to implement your RetryPolicy for your requests. By default, Volley sets all socket and connection timeouts to 5 seconds for all requests.
Calling #add (com.android.volley.Request) will enqueue the given Request for dispatch, resolving from either cache or network on a worker thread, and then delivering a parsed response on the main thread. Adds a Request to the dispatch queue. Cancels all requests in this queue with the given tag. Tag must be non-null and equality is by ident
You should set the request's RetryPolicy: myRequest.setRetryPolicy (new DefaultRetryPolicy ( MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); If you are only just getting started with Volley, you might want to instead consider droidQuery, which is a little easier to configure:
But you already have 2 listeners for your Volley requests: the success listener and the error listener. You can fetch you result (s) in the success listener (and maybe set some custom value in your error listeners - depending on your needs) and just launch your subsequent requests from there - the success and/or error listeners.
You should set the request's RetryPolicy:
myRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
This would change your code to:
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);
If you are only just getting started with Volley, you might want to instead consider droidQuery, which is a little easier to configure:
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());
}
}));
Something like this
RetryPolicy retryPolicy = new DefaultRetryPolicy(
YOUR_TIMEOUT_MS,
YOUT_MAX_RETRIES,
YOUR_BACKOFF_MULT
);
JsonObjectRequest request = new JsonObjectRequest(...);
request.setRetryPolicy(retryPolicy);
Or you could implement your own RetryPolicy
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With