Android网络通讯架构——Volley
1、 Volley提供的功能
简单来说,它提供了如下的便利功能:
- JSON,图像等的异步下载;
- 网络请求的排序(scheduling)
- 网络请求的优先级处理
- 缓存
- 多级别取消请求
- 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求)
2、学习前的准备:下载volley源码打成jar包(我通过这个网址下载不下了,但是网络上很多好心人已经分享了源码)
下载地址:
mirror of volley library from android source
from :
https://android.googlesource.com/platform/frameworks/volley
original:
git clone https://android.googlesource.com/platform/frameworks/volley
update weekly from android source repo
3、正式开工:
简单例子:
private void getStringVolley() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest sRequest=new StringRequest(Request.Method.GET,CITY_CODE_URL, new Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
System.out.println("sorry,Error");
}
});
sRequest.setShouldCache(false);
requestQueue.add(sRequest);
}