排球-串行请求而不是并行请求? [英] Volley - serial requests instead of parallel?

查看:71
本文介绍了排球-串行请求而不是并行请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用volley来从我的Android应用程序发出HTTP请求. 这是我正在使用的代码:

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?

有可能吗?

谢谢.

推荐答案

这可以通过创建一个线程池为1的请求来完成.

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);
}

在getNetwork()方法中

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);
}

,我们使用start方法start()请求队列.

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

在这种情况下,将首先处理结果requestTwo,然后分别处理requestOnerequestThree.如果您不想阻止请求处理,也可以使请求串行发生,这将很有帮助.

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.

这篇关于排球-串行请求而不是并行请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆