Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley - serial requests instead of parallel?

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.

like image 222
sharonooo Avatar asked May 10 '15 08:05

sharonooo


2 Answers

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

like image 139
inmyth Avatar answered Oct 20 '22 07:10

inmyth


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.

like image 34
Zaid Hoona Avatar answered Oct 20 '22 07:10

Zaid Hoona