Volley,volley架構
一 網路請求
1.get方式請求資料
// 1 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);// 2 建立一個請求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {// 正確接收資料回調@Overridepublic void onResponse(String s) {tv_volley_result.setText(s);}}, new Response.ErrorListener() {// 發生異常後的監聽回調@Overridepublic void onErrorResponse(VolleyError volleyError) {tv_volley_result.setText("載入失敗" + volleyError);}});// 3 將建立的請求添加到請求隊列中requestQueue.add(stringRequest);
2.post方式請求資料
// 1 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);// 2 建立一個post請求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {@Overridepublic void onResponse(String s) {tv_volley_result.setText(s);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError volleyError) {tv_volley_result.setText("請求失敗" + volleyError);}}) {@Overrideprotected Map<String, String> getParams() throws AuthFailureError {Map<String, String> map = new HashMap<String, String>();// map.put("value1","param1");return map;}};// 3 將post請求添加到隊列中requestQueue.add(stringRequest);
3.請求Json資料
// 1 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);// 2 建立一個請求String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api";JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject jsonObject) {tv_volley_result.setText(jsonObject.toString());}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError volleyError) {tv_volley_result.setText("請求失敗" + volleyError);}});// 3 將建立的請求添加到請求隊列中requestQueue.add(jsonObjectRequest);
二 圖片載入
1.ImageRequest載入圖片
// 1 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);// 2 建立一個圖片的請求String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {@Overridepublic void onResponse(Bitmap bitmap) {// 正確接收到圖片iv_volley_result.setImageBitmap(bitmap);}}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError volleyError) {iv_volley_result.setImageResource(R.drawable.atguigu_logo);}});// 3 將請求添加到請求隊列中requestQueue.add(imageRequest);
2.Imageloader載入圖片
// 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());// 載入圖片String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";ImageLoader.ImageListener imageListener = imageLoader.getImageListener(iv_volley_result, R.drawable.atguigu_logo, R.drawable.atguigu_logo);imageLoader.get(url, imageListener);
BitmapCache.java
1 public class BitmapCache implements ImageLoader.ImageCache { 2 private LruCache<String, Bitmap> mCache; 3 4 public BitmapCache() { 5 int maxSize = 10 * 1024 * 1024;// 10m 6 mCache = new LruCache<String, Bitmap>(maxSize) { 7 @Override 8 protected int sizeOf(String key, Bitmap bitmap) { 9 return bitmap.getRowBytes() * bitmap.getHeight();10 }11 };12 }13 14 @Override15 public Bitmap getBitmap(String url) {16 return mCache.get(url);17 }18 19 @Override20 public void putBitmap(String url, Bitmap bitmap) {21 mCache.put(url, bitmap);22 }23 }
BitmapCache
3.NetworkImageView載入圖片
<com.android.volley.toolbox.NetworkImageViewandroid:id="@+id/iv_volley_networkimagview"android:layout_width="200dp"android:layout_height="200dp"android:visibility="gone"></com.android.volley.toolbox.NetworkImageView>
// 建立一個請求隊列RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this);// 建立一個ImageloaderImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());// 預設圖片和異常圖片設定iv_volley_networkimagview.setDefaultImageResId(R.drawable.atguigu_logo);iv_volley_networkimagview.setErrorImageResId(R.drawable.atguigu_logo);// 載入圖片String url = "http://img5.mtime.cn/mg/2016/10/11/160347.30270341.jpg";iv_volley_networkimagview.setImageUrl(url, imageLoader);