您的位置:首页 > 其它

文章标题

2016-09-25 16:52 141 查看

配置一个 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 的重创而重建(例如,当用户旋转手机屏幕时)

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

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: