
正文
Volley使用指南第二回(来自developer.android)
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
上一篇文章翻译了一下google的Volley官方文档,讲到了最基本的发送request。这一次我们来下一回:创建一个自定义RequestQueue。
这篇文章将会教你一步一步创建自己的RequestQueue,并定制你自己的行为。
第一步:使用定制的Network和cache
一个RequestQueue需要两样东西去完成他的工作:即发送请求的network和处理缓存的cache。在Volley中有两个实现的标准接口:那么DiskBasedCache提供一个
一个response对应一个文件的缓存和内存级别的索引。BasicNetwork提供基于Http client的网络传输。
BasicNetwork是Volley默认网络的网络接口,它必须被一个你正在使用的Http client初始化才能使用。就像下面这样:
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);
如果你只是一次性请求并且想要使用线程池,你可以在任意你想要的时候创建RequestQueue,然后再数据返回或者出错后调用stop()方法。但是更常用的方式是以单例的
方式去创建RequestQueue保证它运行在你的应用的生命周期内。
第二步:使用单例模式
如果你的应用持续使用网络,那么最好使用单例模式。实现它有很多种方式,推荐使用实现一个封装好RequestQueue和Volley其他功能的类。另一种方法是继承Application
并且在你的Application.onCreate()方法中创建
RequestQueue
,但是这种方法是不被推荐的。一个单例可以实现同样的功能并且更加符合模块化的设计。
重要的一点是RequestQueue必须使用Application context而不是一个Activity Context。这可以保证RequestQueue在app的整个生命周期内都有效,而不是每次重新创建
Activity都要重新创建(比如手机从横屏到竖屏)。
下面是一个栗子:
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;
}
}
// 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);






