前一篇粗略的介绍了一下Volley,并在最后附上了一段使用代码,这一篇详细的介绍一下Volley的使用。前面也说了Volley主要获取JSON对象和图片加载,这里也分为两部分介绍。
private RequestQueue mRequestQueue;
mRequestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i(TAG,response.toString()); parseJSON(response); va.notifyDataSetChanged(); pd.dismiss(); } },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG,error.getMessage()); } }); mRequestQueue.add(jr);
JsonObjectRequest:返回JSON对象。
JsonArrayRequest:返回JsonArray。
StringRequest:返回String,这样可以自己处理数据,更加灵活。
另外可以继承Request<T>自定义Request。
@Override public void onStop() { for (Request <?> req : mRequestQueue) { req.cancel(); } }
@Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); mRequestQueue.cancelAll(this); }
@Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); mRequestQueue.cancelAll( new RequestFilter() {}); or mRequestQueue.cancelAll(new Object()); }
singleImg=(ImageView)findViewById(R.id.volley_img_single_imgeview); ImageRequest imgRequest=new ImageRequest(url, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap arg0) { // TODO Auto-generated method stub singleImg.setImageBitmap(arg0); } }, 300, 200, Config.ARGB_8888, new ErrorListener(){ @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub } }); mRequestQueue.add(imgRequest);
RequestQueue mRequestQueue = Volley.newRequestQueue(this); final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>( 20); ImageCache imageCache = new ImageCache() { @Override public void putBitmap(String key, Bitmap value) { mImageCache.put(key, value); } @Override public Bitmap getBitmap(String key) { return mImageCache.get(key); } }; ImageLoader mImageLoader = new ImageLoader(mRequestQueue, imageCache); // imageView是一个ImageView实例 // ImageLoader.getImageListener的第二个参数是默认的图片resource id // 第三个参数是请求失败时候的资源id,可以指定为0 ImageListener listener = ImageLoader .getImageListener(imageView, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete); mImageLoader.get(url, listener);
public class NetworkImageView extends ImageView继承自ImageView,添加了一个
public void setImageUrl(String url, ImageLoader imageLoader) {}方法,参数包含一个Url地址和一个ImageLoader对象
private void loadImageIfNecessary(final boolean isInLayoutPass) {}内部实现,boolean 类型参数代表是否重新请求网络 ,true:重新请求 false:取缓存
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); //onLayout时重新请求 loadImageIfNecessary(true); } @Override protected void onDetachedFromWindow() { //销毁View的时候Release操作 if (mImageContainer != null) { // If the view was bound to an image request, cancel it and clear // out the image from the view. mImageContainer.cancelRequest(); setImageBitmap(null); // also clear out the container so we can reload the image if necessary. mImageContainer = null; } super.onDetachedFromWindow(); } //drawable状态改变重绘 @Override protected void drawableStateChanged() { super.drawableStateChanged(); invalidate(); }
/** * Sets the default image resource ID to be used for this view until the attempt to load it * completes. */ public void setDefaultImageResId(int defaultImage) { mDefaultImageId = defaultImage; } /** * Sets the error image resource ID to be used for this view in the event that the image * requested fails to load. */ public void setErrorImageResId(int errorImage) { mErrorImageId = errorImage; }