标签:over exp resource conf blocks fail prot indicator default
一 网络请求
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>() { // 正确接收数据回调 @Override public void onResponse(String s) { tv_volley_result.setText(s); } }, new Response.ErrorListener() {// 发生异常后的监听回调 @Override public 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>() { @Override public void onResponse(String s) { tv_volley_result.setText(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_volley_result.setText("请求失败" + volleyError); } }) { @Override protected 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>() { @Override public void onResponse(JSONObject jsonObject) { tv_volley_result.setText(jsonObject.toString()); } }, new Response.ErrorListener() { @Override public 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>() { @Override public void onResponse(Bitmap bitmap) { // 正确接收到图片 iv_volley_result.setImageBitmap(bitmap); } }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() { @Override public 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 @Override 15 public Bitmap getBitmap(String url) { 16 return mCache.get(url); 17 } 18 19 @Override 20 public void putBitmap(String url, Bitmap bitmap) { 21 mCache.put(url, bitmap); 22 } 23 }
3.NetworkImageView加载图片
<com.android.volley.toolbox.NetworkImageView android: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); // 创建一个Imageloader ImageLoader 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);
标签:over exp resource conf blocks fail prot indicator default
原文地址:http://www.cnblogs.com/ganchuanpu/p/6021414.html