Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley RequestQueue Timeout

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 ??

like image 764
Rizvan Avatar asked Jan 22 '14 07:01

Rizvan


People also ask

How do I handle volley timeouts?

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.

What is the use of call () function in volley request?

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

How do I set the retrypolicy for volley requests?

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:

How to get result from multiple volley requests at once?

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.


2 Answers

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());
                            }
                        }));
like image 107
Phil Avatar answered Oct 19 '22 08:10

Phil


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.

like image 7
Blaz Avatar answered Oct 19 '22 09:10

Blaz