Omer KAPLANDROID
Omer KAPLANDROID

Reputation: 1105

Volley How to order Requests

i want to call 3 service methods (A B and C) back to back. The important point is B must be called after response received from A and same situation between B and C as well. I add all of requests to queue using RequestQueue.add(...). But now request B is called before receiving response from A. Is it possible to manage this using volley library.

I know i can do request B after receiving response from A but i want to know can volley does this work.

Upvotes: 0

Views: 2897

Answers (2)

Dmytro Danylyk
Dmytro Danylyk

Reputation: 19798

You can't give request an order, but you can make them run one after another. For this you need to implement your own RequestQueue.

Here is sample which demonstrates how to make all your requests execute in the same order, in which you added them to queue, since it uses single thread execution.

// Copied from Volley.newRequestQueue(..); source code
File cacheDir = new File(context.getCacheDir(), "def_cahce_dir");

String userAgent = "volley/0";
try {
    String packageName = context.getPackageName();
    PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
    userAgent = packageName + "/" + info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
}

if (stack == null) {
    if (Build.VERSION.SDK_INT >= 9) {
        stack = new HurlStack();
    } else {
        // Prior to Gingerbread, HttpUrlConnection was unreliable.
        // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
        stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
    }
}

int threadPoolSize = 1; // means only one request at a time
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network, threadPoolSize);
queue.start();

Upvotes: 2

kjurkovic
kjurkovic

Reputation: 4104

You can implement your own Response listener so you can call A to B and B to C in the response callback method.

There is a simple example here: https://stackoverflow.com/a/17278867/508126

Volley can't do it itself but it can do it if you implement Response.Listener and add your logic in it

Upvotes: 2

Related Questions