Volly的使用
mQueue = Volley.newRequestQueue(getApplicationContext()); //StringRequest四个构造参数分别是Request类型,url,网络请求响应监听器,错误监听器 mQueue.add(new StringRequest(Method.GET, "http://www.baidu.com/", new Listener<String>(){ @Override public void onResponse(String arg0) { // TODO Auto-generated method stub Log.e("onResponse", arg0); } }, new ErrorListener(){ @Override public void onErrorResponse(VolleyError arg0) { // TODO Auto-generated method stub Log.e("onErrorResponse", arg0.toString()); } })); mQueue.start();
这是比较普通的用法,利用静态方法获得对了,然后添加请求,然后start。
请求官方提供有三种请求的封装,一个是StringRequest,一个事ImageRequest,还有一个是JsonRequest。
然而在IKE那里也看到了另一种用法。
private static Cache openCache() { return new DiskBasedCache(CacheUtils.getExternalCacheDir(AppData.getContext()), 10 * 1024 * 1024); } private static RequestQueue newRequestQueue() { RequestQueue requestQueue = new RequestQueue(openCache(), new BasicNetwork(new HurlStack())); requestQueue.start(); return requestQueue; }
自己直接调用构造方法,并设置了缓存。
然后是ImageLoad的使用。
private static ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache( MEM_CACHE_SIZE));
利用下面的方法即可做到异步加载
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 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);
关于图片,还可自定义:
public static ImageLoader.ImageListener getImageListener(final ImageView view, final Drawable defaultImageDrawable, final Drawable errorImageDrawable) { return new ImageLoader.ImageListener() { @Override public void onErrorResponse(VolleyError error) { if (errorImageDrawable != null) { view.setImageDrawable(errorImageDrawable); } } @Override public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { if (response.getBitmap() != null) { if (!isImmediate && defaultImageDrawable != null) { TransitionDrawable transitionDrawable = new TransitionDrawable( new Drawable[] { defaultImageDrawable, new BitmapDrawable(AppData.getContext().getResources(), response.getBitmap()) }); transitionDrawable.setCrossFadeEnabled(true); view.setImageDrawable(transitionDrawable); transitionDrawable.startTransition(100); } else { view.setImageBitmap(response.getBitmap()); } } else if (defaultImageDrawable != null) { view.setImageDrawable(defaultImageDrawable); } } }; }
以上内容来自:
https://github.com/Issacw0ng/Dribbo
http://blog.csdn.net/xyz_lmn/article/details/12063561
http://www.cnblogs.com/bvin/p/3253544.html