文章标题

配置一个 RequestQueue

先前的课程展示了利用 Volley 的默认行为优势如何使用便利方法 Volley.newRequestQueue 来配置一个 RequestQueue。这节课带你显式地创建一个 RequestQueue,为了配置自定义行为。

这节课也描述了推荐使用一个 singleton(单例) 来创建一个 App 生命周期级的的 RequestQueue

配置一个网络和缓存

一个 RequestQueue 的工作需要做两件事情:一个 network 来执行 request 传输,一个 cache 来处理缓存。在 Volley toolbox 中有这些标准的实现:DiskBasedCache 提供一个 one-file-per-response(一个响应对应一个文件)和一个 in-memory index,BasicNetwork 也提供了一个基于你偏好的 HTTP client 网络传输实现

BasicNetwork 是 Volley 提供的默认网络实现,一个BasicNetwork 必须使用你 App 用来连接网络的 HTTP client 来初始化。尤其是一个 HttpURLConnection

这个代码片段展示了配置一个 RequestQueue 的步骤:

RequestQueue mRequestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);

// Start the queue
mRequestQueue.start();

String url ="http://www.example.com";

// Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
    new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    // Do something with the response
}
},
new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // Handle error
}
});

// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);

// ...

如果只需要一次 request,并且不想常驻这个 thread pool,你可以在任何地方创建 RequestQueue 然后一旦请求 response 或者错误返回时调用 stop() 方法,使用在 Sending a Simple Request里描述的 Volley.newRequestQueue 那样。但是更常见的使用情况是使用单例创建一个 RequestQueue 确保它能够在整个 App 生命周期中可以运行,就像下面描述的那样。

使用一个 Singleton Pattern (单例模式)

如果你的应用需要频繁地使用网络,设置一个 RequestQueue 单例对象可能是最有效的方法来使之在你的 App 整个生命周期中都有效。你可以采用不同的方式,这里推荐的方法是实现单例类来封装 RequestQueue 和其他 Volley 方法。另一个方法是创建一个 Application 子类然后在 [Application.onCreate()] 方法中设置一个 RequestQueue,但是这种方法不推荐使用,一个静态的单例类可以以更加模块化的方法来提供相同的功能实现

初始化 RequestQueue 的一个关键的概念是需要使用 Application 的 context,不是一个 Activity 的 context。这样就会确保
RequestQueue 会被持续在 App 的整个生命周期中,而不是每次随activity 的重创而重建(例如,当用户旋转手机屏幕时)

这里有一个提供 RequestQueueImageLoader 功能的单例类:

public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private MySingleton(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap>
                cache = new LruCache<String, Bitmap>(20);

        @Override
        public Bitmap getBitmap(String url) {
            return cache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            cache.put(url, bitmap);
        }
    });
}

public static synchronized MySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new MySingleton(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(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}
}

这里有一些使用单例类来执行 RequestQueue 操作的例子:

// Get a RequestQueue
RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).
getRequestQueue();

// ...

// Add a request (in this example, called stringRequest) to your RequestQueue.
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值