Volley使用了线程池来作为基础结构,主要分为主线程,cache线程和network线程。主线程和cache线程都只有一个,而NetworkDispatcher线程可以有多个,这样能解决比并行问题。具体可以参考下图,此图节选自Google I/O 演讲。
引入Volley非常简单,首先,从git库先克隆一个下来:
mRequestQueue = Volley.newRequestQueue(this);
String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";
pd = ProgressDialog.show(this,"Please Wait...","Please Wait...");
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener() {
@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);
RequestQueue mRequestQueue = Volley.newRequestQueue(this);
final LruCache mImageCache = new LruCache(
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("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg", listener);
NetworkImageView是继承自ImageView,是Volley提供的一个全新的简单加载图片的控件。
NetworkImageView netImgView=(NetworkImageView)findViewById(R.id.volley_img_networkimgeview);
netImgView.setTag("url");
netImgView.setImageUrl("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg",mImageLoader);
NetworkImageView是继承自ImageView,是Volley提供的一个全新的简单加载图片的控件。
NetworkImageView netImgView=(NetworkImageView)findViewById(R.id.volley_img_networkimgeview);
netImgView.setTag("url");
netImgView.setImageUrl("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg",mImageLoader);
private RequestQueue mRequestQueue;
mRequestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener() {
@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
rotected 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() {
@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 mImageCache = new LruCache(
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
public void setImageUrl(String url, ImageLoader imageLoader) {}
方法,参数包含一个Url地址和一个ImageLoader对象private void loadImageIfNecessary(final boolean isInLayoutPass) {}
@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;
}
NetworkImageView是继承自ImageView,是Volley提供的一个全新的简单加载图片的控件。
NetworkImageView netImgView=(NetworkImageView)findViewById(R.id.volley_img_networkimgeview);
netImgView.setTag("url");
netImgView.setImageUrl("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg",mImageLoader);