Volley
- 好处
- Automatic scheduling of network requests.
- Multiple concurrent network connections.
- Transparent disk and memory response caching with standard HTTP cache coherence.
- Support for request prioritization.
- Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
- Ease of customization, for example, for retry and backoff.
- Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
- Debugging and tracing tools.
Volley excels at RPC-type operations used to populate a UI, such as fetching a page of search results as structured data. It integrates easily with any protocol and comes out of the box with support for raw strings, images, and JSON.
-
缺点
Volley不擅长下载大文件以及流操作,因为它在解析的数据都全部保存在内存中。大文件下载还是使用
DownloadManager
向网络发送简单的请求
使用Volley 创建一个请求队列 RequestQueue
并将 请求 Request
传给它。请求队列会在网络访问时执行工作线程,在缓存里读写,解析响应。所有的请求都会被解析成原始的响应,而Volley会将解析好的响应转发给原来的主线程进行处理。
- 添加网络权限
使用Volley需要 权限 android.permission.INTERNET
- 使用 newRequestQueue 创建请求队列
Volley 提供 Volley.newRequestQueue
来创建请求队列 RequestQueue
例子:
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// 实例化 RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// 请求从指定的URL里取得字符串
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// 将请求添加进请求队列
queue.add(stringRequest);
Volley总是将已经解析好的 响应responses 转发给主线程,使得用获得的数据来填充你的UI更加方便,你可以自由决定如何处理响应来修改你的UI,但对于某些Volley库所提供的方法例如取消请求来说,特别危险。
- 发送请求
简单 构建一个请求 request
并使用RequestQueue
的 add()
方法就将请求发送出去了。
但你调用 add()
方法时,Volley 会启动一个缓存处理线程和一个网络分发线程池。当你往队列里添加请求时,Volley会尝试从缓存执行线程里拿,如果能从缓存里拿到,那么就在缓存执行线程里解析响应,并将解析好的响应转发给主线程。如果请求不能从缓存线程里拿到,那就把它放到网络队列中。网络队列中第一个有效的网络线程中执行其请求,执行Http事务,在工作线程中解析响应,向缓存中写入响应然后将解析好的响应转发给主线程。
- 取消请求
取消请求只需在Request
对象中里调用cancel()
方法就可以了。一旦调用,Volley保证不会调用你的response handler 。这意味着你可以在Activity
的onStop()
方法中取消所有未发生的request
,and you don't have to litter your response handlers with checks for getActivity() == null, whether onSaveInstanceState() has been called already, or other defensive boilerplate.
为了能在随时取消他们最好跟踪所有正在处理的请求。方法很简单,为每一个请求绑定一个tag
对象。例如:在Activity
标记所有的请求,然后在onStop()
方法中调用requestQueue.cancelAll(this)
取消全部。
下面是具体例子: 1. 定义一个tag
并添加给request
public static final String TAG = "MyTag";
StringRequest stringRequest; // 假设存在
RequestQueue mRequestQueue; //假设存在
// 设置标签
stringRequest.setTag(TAG);
// 将请求添加到请求队列中
mRequestQueue.add(stringRequest);
- 在
Activity
的onStop()
方法中取消所有TAG标签的请求
@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
mRequestQueue.cancelAll(TAG);
}
}
注意:你需要额外注意当你的response handler 是要控制状态或是开始其他的进程的时候。因为,一旦取消,response handler 永远不会被调用。