Volley - serial requests instead of parallel?
This can be done by creating a request with the thread pool as 1.
int MAX_SERIAL_THREAD_POOL_SIZE = 1;
final int MAX_CACHE_SIZE = 2 * 1024 * 1024; //2 MB
private static RequestQueue prepareSerialRequestQueue(Context context) {
Cache cache = new DiskBasedCache(context.getCacheDir(), MAX_CACHE_SIZE);
Network network = getNetwork();
return new RequestQueue(cache, network, MAX_SERIAL_THREAD_POOL_SIZE);
}
in getNetwork() method
private static Network getNetwork() {
HttpStack stack;
if(Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
String userAgent = "volley/0";
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
return new BasicNetwork(stack);
}
and us the start method to start() the request queue.
RequestQueue serialRequestQueue = prepareSerialRequestQueue(context);
serialRequestQueue.start();
Now add the request to this queue intead of the volleys request queue. For example:
StringRequest requestOne = new StringRequest(Request.Method.GET, "http://www.example1.com", this, this);
StringRequest requestTwo = new StringRequest(Request.Method.GET, "http://www.example2.com", this, this);
StringRequest requestThree = new StringRequest(Request.Method.GET, "http://www.example3.com", this, this);
serialRequestQueue.add(requestTwo);
serialRequestQueue.add(requestOne);
serialRequestQueue.add(requestThree);
in this case the result requestTwo
will be handled first followed by requestOne
and requestThree
respectively. This helps if you dont want to block the request processing, and also make them happen serially.
If you want to run parallel requests then you just keep adding requests to the queue. Requests are by default asynchronous.
If you want to run serial requests then there are two approaches.
One is to add the second request inside the first request's onResponse
. This will basically chain multiple asynchronous requests.
Two is to use RequestFuture
which is blocking. For example executing this method will block until response is obtained.
private String blockingRequest(String url) {
RequestFuture<String> future = RequestFuture.newFuture();
StringRequest sr = new StringRequest(url, future, future);
queue.add(sr);
String response = null;
try {
response = future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return response;
}
To use basically you stack multiple blocking request methods like this:
String response1 = blockingRequest1(url1);
// other methods
String response2 = blockingRequest2(url2);
blockingRequest2
will be executed after blockingRequest1
has completed. Without Future both methods will be executed simultaneously. Needless to say you will need to run blocking methods not in UI thread (e.g Thread, AsyncTask, and personal favorite RxJava Observable).