Volley

Volley

  • 好处
    1. Automatic scheduling of network requests.
    2. Multiple concurrent network connections.
    3. Transparent disk and memory response caching with standard HTTP cache coherence.
    4. Support for request prioritization.
    5. Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.
    6. Ease of customization, for example, for retry and backoff.
    7. Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.
    8. 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 。这意味着你可以在ActivityonStop()方法中取消所有未发生的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);
  1. ActivityonStop() 方法中取消所有TAG标签的请求
@Override
protected void onStop () {
    super.onStop();
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(TAG);
    }
}

注意:你需要额外注意当你的response handler 是要控制状态或是开始其他的进程的时候。因为,一旦取消,response handler 永远不会被调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值