I have a for loop , which is creating different url and then i am pushing the urls in the Volley RequestQueue using add function. I have a question that , will the response listner will be responding in the order of the request added to the RequestQueue or it will be random based on the server responses of the requests made? I am using VolleySingleton approach.
4 Answers
Volley RequestQueue class manage asynchonous requests queue.
It means Volley send requests on a FIFO (first in first out) model, but since responses can be quite long to come back, it handle responses in no particular order. You can't use it if you want the result of a first request to be used in a second request.
However, you can visibly use RequestFuture class to use Volley on a synchonous model:

- 3,642
- 3
- 25
- 42

- 95
- 7
-
Pierre, Yes, I want my result from my first request to be used in the 2nd request. Then the result from the 2nd request I want used in the 3rd request. Please, how do I chain such Volley requests? – JDOaktown Sep 19 '16 at 22:16
By default, Volley RequestQueue
will use 4 concurrent Threads to process the Requests
added to the Queue
. This may be changed by extending the RequestQueue
class and passing the desired Thread pool size as the third parameter to super
contructor.
Passing 1 to the constructor will result in the RequestQueue
processing one Request at a time :-)

- 3,642
- 3
- 25
- 42

- 7,024
- 2
- 29
- 46
-
-
1Sure :-) http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/com/android/volley/RequestQueue.java#73 – Kelevandos Sep 26 '16 at 06:30
-
You might want to add this line `DEFAULT_NETWORK_THREAD_POOL_SIZE = 4` to your answer as reference, so it will be more authentic. +1 for the official reference link. – Talha Sep 26 '16 at 06:54
No! Android volley process each requests in request queue Asynchronously:
Refer the architecture image:
Each requests in the request queue will be processed by the network threads and also you can limit the network dispatcher thread count check here

- 3,642
- 3
- 25
- 42

- 1,255
- 13
- 32
-
so how can i distinguish the responses, i have set the tag to the requests, but i cant find out how to identify the responses. ( We can cancel the requests based on the tags, but is there a way to identify the request corresponding to the responses received?) – Vishesh Dec 22 '15 at 12:31
-
@Vishesh i think volley handles all the process of mapping request-response , for each requests a response listener is associated, you can refer here http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – Dinesh Kannan Dec 22 '15 at 12:36
Assuming that the default behavior is to send multiple requests on separate concurrent threads, then the order of the responses then becomes dependent on the total response time for each individual request.
As to identifying the responses (other than the data itself), it seems like you should be able to instantiate a Response.Listener implementation with a unique request identifier (original URL or int) such that when the response handler is invoked it is able to differentiate the original request.
public class JSONProcessor implements Response.Listener<JSONObject> {
private String url;
private int reqCode;
public JSONProcessor (String reqUrl) {
this.url = reqUrl
}
public JSONProcessor (int reqCode) {
this.reqCode = reqCode;
}
public void onResponse(JSONObject response) {
// use this.url or this.reqCode to determine original request
// process response
}
}
Alternatively, use different implementations of Response.Listener for each endpoint you expect to be processing.
In general, in my opinion it is best to be completely data driven if at all possible.