排球 - 串行请求而不是并行?

sha*_*ooo 10 android android-volley

我正在使用来自我的Android应用程序的volleymake HTTP请求.这是我正在使用的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想向队列添加请求但控制它们的执行方式,例如:

在任意时间向池中添加多个请求,池将只执行第一个请求(队列头),然后在完成时将执行下一个请求......依此类推......

也许有办法使双方执行串行和并行请求?

可能吗?

谢谢.

Zai*_*ona 20

这可以通过创建线程池为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);
}
Run Code Online (Sandbox Code Playgroud)

在getNetwork()方法中

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);
}
Run Code Online (Sandbox Code Playgroud)

和我们一起启动()请求队列的start方法.

RequestQueue serialRequestQueue = prepareSerialRequestQueue(context);
serialRequestQueue.start();
Run Code Online (Sandbox Code Playgroud)

现在将请求添加到volleys请求队列的此队列intead.例如:

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);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,结果requestTwo将首先处理,requestOne然后requestThree分别处理.如果您不想阻止请求处理,并且还使它们连续发生,这会有所帮助.


inm*_*yth 5

如果您想运行并行请求,那么您只需不断将请求添加到队列中即可。请求默认是异步的。

如果您想运行串行请求,那么有两种方法。

一种是将第二个请求添加到第一个请求的onResponse. 这基本上会链接多个异步请求。

二是利用RequestFuture阻塞。例如,执行此方法将阻塞,直到获得响应。

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;
}
Run Code Online (Sandbox Code Playgroud)

基本上,您可以堆叠多个阻塞请求方法,如下所示:

String response1 = blockingRequest1(url1);
// other methods
String response2 = blockingRequest2(url2);
Run Code Online (Sandbox Code Playgroud)

blockingRequest2blockingRequest1完成后将被执行。如果没有 Future,这两个方法将同时执行。不用说,您将需要不在UI 线程中运行阻塞方法(例如 Thread、AsyncTask 和个人最喜欢的 RxJava Observable)。