Volley - serial requests instead of parallel?
Asked Answered
Z

3

10

I'm using volley for make HTTP requests from my Android application. Here is the code I'm using:

public class RequestPool {


    private static RequestPool mInstance;

    private RequestQueue mRequestQueue;

    private static Context mContext;

    private RequestPool(Context context) {

        mContext = context;
        mRequestQueue = getRequestQueue();


    }

    public static synchronized RequestPool getInstance(Context context) {

        if (mInstance == null) {
            mInstance = new RequestPool(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
            mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {

        getRequestQueue().add(req);
    }
}

I want to add requests to the queue but control the way they are executed, for example:

adding multiple requests to the pool at arbitrary times, the pool will execute only the first request (head of queue), then when finishes will execute the next one ... and so on....

Maybe there is a way to make both serial and parallel requests executing?

Is it possible?

Thanks.

Zambia answered 10/5, 2015 at 8:48 Comment(0)
G
21

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.

Gibe answered 24/7, 2015 at 6:41 Comment(3)
You can get the gist hereGibe
can you help me ? my question is how can i handle response all 3 requestNomi
There are parameters in a function when you create a request, you can supply the parameter there. It is Response.Listener parameter that will help you do it. You can see here, see how the StringRequest is being create you will get the answer.Gibe
D
5

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).

Decapod answered 10/5, 2015 at 10:13 Comment(3)
Thank you for the quick response. Actually I'm already using the first approach. The issue is that there's a multiple threads which adding request into the request pool. So i might use both methods you mentioned .. Any chance to get the usage of the 'blockingRequest'?Zambia
There is not much story but I edited my answer to illustrate it. RequestQueue as the name suggests, only queues requests in first in first served (FIFO) order. It's already synchronized so you don't need to worry about accessing it from multiple threads.Decapod
They are in parallel, but I'll point out that the number of simultaneously executing requests is controlled by a thread pool size which is by default 4 (DEFAULT_NETWORK_THREAD_POOL_SIZE in RequestQueue)Soldo
R
0

This is what I am using:

private static RequestQueue newRequestQueue(Context context) {
    File cacheFile = new File(context.getCacheDir(), "volley");
    Cache cache = new DiskBasedCache(cacheFile);
    RequestQueue queue = new RequestQueue(cache, new BasicNetwork(new HurlStack()), 1);
    queue.start();
    return queue;
}
Roid answered 13/4, 2020 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.