首先,引入Volley类库,添加相关权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Volley中的核心类
创建请求队列RequestQueue
RequestQueue queue = Volley.newRequestQueue(this);
使用StringRequest进行请求
//2.创建网络请求 StringRequest stringRequest = new StringRequest(url, new Listener<String>() { @Override public void onResponse(String response) { tv_result.setText(response); } },new MyErrorListener()); //3.执行请求 queue.add(stringRequest);
使用JsonRequest进行请求
//1.创建JsonRequest请求 JsonObjectRequest joRequest = new JsonObjectRequest(url, null, new Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { tv_result.setText(response.toString()); } }, new MyErrorListener()); //2.执行请求 queue.add(joRequest);
使用JsonArrayRequest进行请求
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(listUrl, new Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { tv_result.setText(response.toString()); } }, new MyErrorListener()); queue.add(jsonArrayRequest);
使用ImageRequest进行请求
ImageRequest imageRequest = new ImageRequest(imageUrl,new Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { iv_result.setImageBitmap(response); } }, 200, 100, Config.RGB_565, new MyErrorListener()); queue.add(imageRequest);
第一步: 从原图进行等宽高比的采样,采样的值最好是2的倍数,代码如下:
Options opts = new Options(); opts.inSampleSize = 4; Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fast, opts);
第二步: 根据图片的原始宽高比和控件的宽高比,科学的计算采样比例,代码如下:
Options opts = new Options(); opts.inJustDecodeBounds = true;//设置只解析图片的边界参数,即宽高 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fast, opts); //科学计算图片所需的采样比例 opts.inSampleSize = caculateSampleSize(opts.outWidth,opts.outHeight); opts.inJustDecodeBounds = false;//关闭标记,解析真实的图片 bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fast, opts); /** * 根据图片的原始宽高比和ImageView的宽高计算图片的采样比例 * @param outWidth * @param outHeight * @return */ private int caculateSampleSize(int outWidth, int outHeight) { int inSampleSize = 1; if(outWidth>outHeight){ //参考宽进行缩放 inSampleSize = outWidth/iv_result.getWidth();//1920/300 }else{ //参考高进行缩放 inSampleSize = outHeight/iv_result.getHeight(); } if(inSampleSize<1){ inSampleSize = 1; } return inSampleSize; }
第三步: 设置图片加载的渲染模式为Config.RGB_565,能降低一半内存:
opts.inPreferredConfig = Config.RGB_565
使用ImageLoader加载图片
protected void loadImage() { ImageListener imageListener = ImageLoader.getImageListener(iv_result, R.drawable.ic_launcher, R.drawable.ic_launcher); MemoryCache imageCache = new MemoryCache(); ImageLoader imageLoader = new ImageLoader(queue, imageCache); imageLoader.get(imageUrl, imageListener); } /** * 图片内存缓存 * @author lxj * */ public class MemoryCache implements ImageCache{ private LruCache<String, Bitmap> lruCache; //app可用内存的8分之一 private int maxSize = (int) (Runtime.getRuntime().totalMemory()/8); public MemoryCache(){ lruCache = new LruCache<String, Bitmap>(maxSize){ @Override protected int sizeOf(String key, Bitmap value) { return value.getHeight()*value.getRowBytes(); } }; } @Override public Bitmap getBitmap(String url) { return lruCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { lruCache.put(url, bitmap); } }
内存缓存详解
在Android2.3之前还没有好的内存缓存策略出现,一般采用SoftRefrence对Bitmap进行包装,能尽量保证不会出现oom,几种引用的解释如下:
软应用:使用SoftRefrence去包装一个对象,内存不足的时候去回收对象,尽量保证不oom,代码如下:
HashMap<String, SoftReference<Bitmap>> map = new HashMap<String, SoftReference<Bitmap>>(); SoftReference<Bitmap> reference = map.get(url); Bitmap bitmap = reference.get();
MemoryCache imageCache = new MemoryCache(); ImageLoader imageLoader = new ImageLoader(queue, imageCache); net_imageview.setImageUrl(imageUrl, imageLoader);
代码如下:
protected void execGsonRequest() { GsonRequest<Stu> gsonRequest = new GsonRequest<Stu>(url, Stu.class, new Listener<Stu>() { @Override public void onResponse(Stu stu) { tv_result.setText(stu.toString()); } }, new MyErrorListener()); queue.add(gsonRequest); } public class GsonRequest<T> extends Request<T>{ private Class<T> clazz; private final Listener<T> mListener; public GsonRequest(String url,Class<T> clazz,Listener<T> listener, ErrorListener errorListener) { super(url, errorListener); mListener = listener; this.clazz = clazz; } @Override protected Response<T> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } //解析json,返回response T t = new Gson().fromJson(parsed,clazz ); return Response.success(t, HttpHeaderParser.parseCacheHeaders(response)); } @Override protected void deliverResponse(T response) { mListener.onResponse(response); } }
给每个Request添加tag标识
stringRequest.setTag(this);
调用取消请求的方法
queue.cancelAll(this);
public class PostReuqest extends StringRequest { private Map<String, String> params; public PostReuqest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) { super(url, listener, errorListener); } public PostReuqest(int method,String url, Response.Listener<String> listener, Response.ErrorListener errorListener) { super(method,url, listener, errorListener); } public void setParams(Map<String, String> params){ this.params = params; } @Override protected Map<String, String> getParams() throws AuthFailureError { return params; } } PostReuqest stringRequest = new PostReuqest(Request.Method.POST,Api.LOGIN, new com.android.volley.Response.Listener<String>() { @Override public void onResponse(String response) { text.setText(response); } }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); HashMap<String,String> map = new HashMap<>(); map.put("username","hehehe"); map.put("password","12321"); stringRequest.setParams(map);
/** * Volley的二次封装类 * @author lxj * */ public class VolleyHelper { private RequestQueue requestQueue; MemoryCache imageCache = new MemoryCache(); private static VolleyHelper mInstance = null; private VolleyHelper(Context context){ requestQueue = Volley.newRequestQueue(context); } public static VolleyHelper get(Context context){ if(mInstance==null){ mInstance = new VolleyHelper(context); } return mInstance; } public <T> void executeRequest(Request<T> request){ requestQueue.add(request); } /** * 执行GsonRequest * @param url * @param clazz * @param listener * @param errorListener */ public <T> void executeGsonRequest(String url,Class<T> clazz,Listener<T> listener,ErrorListener errorListener){ GsonRequest<T> gsonRequest = new GsonRequest<T>(url, clazz, listener, errorListener); gsonRequest.setTag(url); requestQueue.add(gsonRequest); } /** * 取消请求 * @param tag */ public void cancelRequest(String tag){ requestQueue.cancelAll(tag); } /** * 加载图片 * @param imageUrl * @param imageView */ public void loadImage(String imageUrl,ImageView imageView){ ImageListener imageListener = ImageLoader.getImageListener(imageView, R.drawable.ic_launcher, R.drawable.ic_launcher); ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); imageLoader.get(imageUrl, imageListener); } }
原文:http://www.cnblogs.com/yegong0214/p/6498450.html