Volley網路框架之快取載入圖片、Post與get的資料請求
阿新 • • 發佈:2018-11-30
前言:Volley作為主流網路框架之一,必然有它的優點。Volley可是說是把AsyncHttpClient和Universal-Image-Loader的優點集於了一身, 它的常用在資料量不大,但網路通訊頻繁,而且有圖片快取功能。但是如果有大檔案下載,Volley的表現就會非常糟糕,我建議使用okhttp框架,落雨敏下一篇就寫okhttp框架的使用。
1.Volley的網路請求佇列建立與取消佇列請求-----------------------------------------------
建立請求佇列,方便靈活的取消佇列。
public class ApplicationTest extends Application{
public static RequestQueue queues;//請求佇列
@Override
public void onCreate() {
super.onCreate();
queues=Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpQueues(){
return queues;
}
}
2.Volley的Get和Post請求方式的使用---------------------------------------
StringRequest:主要使用在對請求資料的返回型別不確定的情況下,StringRequest涵蓋了JsonObjectRequest和JsonArrayRequest。
private void get_StringRequest() { String url="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443"; StringRequest request=new StringRequest(Method.GET, url, //請求成功回撥 new Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show(); } }, //請求失敗回撥 new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show(); } } ); request.setTag("testGet");//設定請求的Tag標籤,可以在全域性請求佇列中通過Tag標籤進行請求的查詢 ApplicationTest.getHttpQueues().add(request); //將請求加入全域性佇列中 }//get方法end private void post_StringRequest() { String url="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm"; StringRequest request=new StringRequest(Method.POST, url, //請求成功 new Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show(); } }, //請求失敗 new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show(); } } ) { //自動呼叫傳引數方法 @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String,String> map = new HashMap<>(); //將請求引數名與引數值放入map中 map.put("tel","15850781443"); return map; } };// request.setTag("testPost");//設定請求的Tag標籤,可以在全域性請求佇列中通過Tag標籤進行請求的查詢 ApplicationTest.getHttpQueues().add(request); //將請求加入全域性佇列中 }//post方法end JsonObjectRequest:當確定請求資料的返回型別為JsonObject時使用。 private void get_JsonObjectRequest() { String url="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.4.255.255"; //第三個請求引數是JsonObject,由於是get請求,在url中有了,設定null JsonObjectRequest request=new JsonObjectRequest(Method.GET, url, null, new Listener<JSONObject>() { //請求成功 @Override public void onResponse(JSONObject response) { Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show(); } }, //請求失敗 new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show(); } } ); request.setTag("testGet");//設定請求的Tag標籤,可以在全域性請求佇列中通過Tag標籤進行請求的查詢 ApplicationTest.getHttpQueues().add(request); //將請求加入全域性佇列中 }//get方法end private void post_JsonObjectRequest() { String url = "http://www.kuaidi100.com/query"; Map<String,String> map = new HashMap<>(); map.put("type","yuantong"); map.put("postid","229728279823"); JSONObject jsonRequest=new JSONObject(map);//引數 JsonObjectRequest request=new JsonObjectRequest(url, jsonRequest, //請求成功 new Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Toast.makeText(MainActivity.this,response.toString(),Toast.LENGTH_LONG).show(); } }, //請求失敗 new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show(); } }); request.setTag("testPost");//設定請求的Tag標籤,可以在全域性請求佇列中通過Tag標籤進行請求的查詢 ApplicationTest.getHttpQueues().add(request); //將請求加入全域性佇列中 }//post方法end
3.Volley的網路圖片載入-------------------------------------------------
//-----------------------定義快取類-----------------------
public class BitmapCache implements ImageCache{
//LruCache是基於記憶體的快取類
private LruCache<String,Bitmap> lruCache;
//LruCache的最大快取大小
private int max = 10 * 1024 * 1024;
public BitmapCache() {
lruCache = new LruCache<String, Bitmap>(max){
@Override
//快取圖片的大小
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
//超過最大自動回收
@Override
public Bitmap getBitmap(String s) {
return lruCache.get(s);
}
@Override
public void putBitmap(String s, Bitmap bitmap) {
lruCache.put(s,bitmap);
}
}
//-------------------------main方法呼叫--------------------------
private void loadImageWithCache() {
String url = "http://img3.duitang.com/uploads/item/201412/16/20141216233042_B8WXG.jpeg";
ImageLoader loader = new ImageLoader(ApplicationTest.getHttpQueues(), new BitmapCache());
//預設在載入時圖片,載入失敗時圖片
ImageListener listener = loader.getImageListener(image,R.drawable.defult,R.drawable.defult);
//載入及快取網路圖片
loader.get(url,listener);
}
4.Volley的效果圖片-------------------------------------------------
5.總結
免費原始碼下載(包含完善的自定義封裝)
原始碼下載地址:http://download.csdn.net/detail/lin857/9677695