Android (Java), Development, Mobile

Avoid Multiple Requests When Using Volley

android volley library multiple requests android volley library multiple requests

Many developers using Volley must have faced the problem of request being hit twice to the server. And as a result, they might get their request listener being delivered more than once. In many cases, this can mess-up your code’s logic.

The same happened to me when i was trying to make a delete request and updating the UI by deleting that item from the screen. But since, the delete request was hitting twice, i got two response listener one delivering success and other stating that no such item exist. This messed-up my app’s UI as the item got deleted from the screen and at the same time a toast appears on the screen saying “Unable to delete!”.

I checked the logic which seems absolutely fine as i was making only one request. Also, when i checked the logs i saw the request was printed once and the response was delivered twice. This is when i realized that Volley must be making multiple requests to the server. But was still confused about how and why it must be doing that!

After googling and going through stackoverflow, i came to know that every request in Volley uses a default retry policy.

What is the default retry policy used by Volley??

Volley uses the following default policy:

You can find these setting in DefaultRetryPolicy.java. So according to this default policy, Volley tries to wait for the response for 2500 milliseconds, however if the response is not received in this time span then it retries for the number set by the DEFAULT_MAX_RETRIES, i.e., 1. And the DEFAULT_BACKOFF_MULT variable is used to determine exponential time set to socket for every retry attempt. To understand it more deeply, you may refer Retry Policy.

How to configure Volley to stop sending two request to the server?

Now, to stop the multiple request you can configure retry policy for your request object by using the setRetryPolicy() method of the request object. I managed to do this with following code:

With the above code, i reduced the timeout to 20 seconds and set the number of retries to 0.

If above setting dose not work for you, then you may like to revisit your volley caching logic. Because of the soft ttl in volley caching the result gets delivered from the cache and at the same time it queues another network request which also will return a result. And therefore, single request but two different results.

 

You Might Also Like

  • hangui claude

    thx for the heads-up !!….I’ve been working on a project where I use volley to make HTTP calls and I kept on having multiple request as well…..really helpful

    • http://www.techstricks.com Mansha Chuttani

      I am glad that you found it useful. For any further help with Volley, feel free to ask! :)

  • disqus_a50SLmCHEh

    Is it 2 or 20 seconds?

    • Mansha Chuttani

      My Bad.. It should be 20. Thanks for attention to details, have updated the post.

  • Sumit Jain

    Solution Seems promising,..gotta try it. Does volley fetch result from cache even when request method id POST?

    • http://www.techstricks.com Mansha Chuttani

      If you observe the Request class of volley library, then you will notice that mShouldCache is set to true by default. So every request is cached in volley unless you specifically use setShouldCache(true). Some people argue that this is an architectural design bug, but i believe it is clear on its purpose. As volley gives first priority to the headers that are set by server response. So if Cache-Control: no-cache is set in response header, then volley will not cache the result.

      If you want to check that your response is served from cache or network then you may refer solution given on following link

    • Mansha Chuttani

      If you observe the Request class of volley library, then you will notice that mShouldCache is set to true by default. So every request is cached in volley unless you specifically use setShouldCache(true). Some people argue that this is an architectural design bug, but i believe it is clear on its purpose. As volley gives first priority to the headers that are set by server response. So if Cache-Control: no-cache is set in response header, then volley will not cache the result.

      If you want to check that your response is served from cache or network then you may refer solution given on following link

  • Abdul Basith

    Hi,

    How to block the retry request after getting timeout .

    req.setRetryPolicy(new DefaultRetryPolicy(6000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    I set 0 as retry count . but it again sent the one request after getting timeout .

    • Mansha Chuttani

      A lot of people face this problem with volley. It has been a known bug but no resolution in volley updates till now. However a known workaround for resolving this issue is modifying the openConnection() method of the HurlStack class and adding the following line of code to it:

      connection.setChunkedStreamingMode(0);

      For more research on this issue, you can visit the following discussion: https://groups.google.com/forum/#!topic/volley-users/8PE9dBbD6iA

  • Edwin Njoroge

    Thanks Mansha, My app was making two requests to an API, I was not able to handle the first response since it was masked by a response from the second request. I tried the first solution to no avail. but caching as you pointed out did work.

    • Mansha Chuttani

      I am happy that my post worked for you. :)

  • Sam Jain

    It worked sexy mansha, thank you so much.

  • vipin jain

    Hi,

    What is maximum timeout one can set for a volley request.Can we set 2 min or more than that?

    Thanks,
    Vipin

    • Mansha Chuttani

      Yes you can set max time as 2 min or more but you will have to make sure that your server is also configured for the same timeout and it does not give timeout response before 2 mins. Also, If server control is not with you then you can make use of retry policy and run the request for as much time as you want.

      • vipin jain

        Thanks :)

  • Anatoliy Cheshihin

    I don’t understand, default retry policy utilizes constant with value 0, and your trick is to set 0 instead of default constant (which as I said previously is also 0), why it should behave in a different way?

  • Ganesh Giri

    Thanx Mansha..its working fine.