Android Volley框架的使用(三)

来源:互联网 发布:掌握5.0数据库和模型 编辑:程序博客网 时间:2024/06/05 02:17

Volley框架的学习马上就可以“杀青”了,哈哈,有木有点小激动呢,之所以将这个框架分成三篇来写,而且涉及的细节比较多,是因为考虑到后面还要学习几个Android http框架,只要认认真真看完volley框架的学习,后面几个框架的学习简直“易如反掌”。在开始这篇文章之前
建议先看前两篇文章:

Android Volley的使用(一)

Android Volley的使用(二)

这篇主要介绍图片请求以及缓存的问题,这在开发中似乎是最常用到的,所以最后咱们一起来做个Demo来练习一下!

文章结构如下:

Image Request

为了更方便的使用Volley中的图片请求,我们同样先在ApplicationController类中封装一个ImageLoader

public class ApplicationController extends Application{        /*前面写过的方法不再赘述*/        private ImageLoader imageLoader;        ....        public ImageLoader getImageLoader(){            getRequestQueue();            //如果imageLoader为空则创建它,第二个参数代表处理图像缓存的类            if(imageLoader==null){                imageLoader=new ImagerLoader(this.reqQueue,new LruBitmapCache());            }            return this.imageLoader;        }    }    public class LruBitmapCache extends LruCache<String,Bitmap> implements ImageCache{        public static int getDefaultLruCacheSize(){            final int maxMemory=(int)(Runtime.getRuntime().maxMemory/1024);            final int cacheSize=maxMemory/8;            return cacheSize;        }        public LruBitmapCache(){            this(getDefaultLruBitmapCacheSize);        }        public LruBitmapCache(int sizeInKiloBytes){             super(sizeInkiloBytes);        }        @Override        public int sizeOf(String key,Bitmap Value){            return value.getRowBytes()*value.getHeight()/1024;        }        @Override        public Bitmap getBitmap(String url){            return get(url);        }        @Override        public void putBitmap(String url,Bitmap bitmap){            put(url,bitmap);        }    }
  1. 完成上述步骤后,在使用的时候我们首先需要获取ImageLoader对象
 ImageLoader imageLoader=ApplicationController.getInstance().getImageLoader();
  1. 将图片载入ImageView

可以使用Volley自己提供的一个Image视图,NetworkImageView,几行代码就可以搞定

//将NetworkImageView布局在布局文件中NetworkImageView imageView=(NetworkImageView)findViewById(R.id.networkimageview); //需要用到imageLoader imageView.setImageUrl(url,imageLoader);

如果要将图片直接载入ImageView,可以通过以下方法:

ImageLoader imageLoader=ApplicationController.getInstance().getImageLoader();imageLoader.get(url,new ImageListener(){   @Override   public void onResponse(ImageContainer response,boolean arg)  {        if(response.getBitmap()!=null){        //设置imageView        imageView.setImageBitmap(response.getBitmap());        }    }    @Override    public void onErrorResponse(VolleyError error){        Log.e(TAG,"Image Error"+error.getMessage());        }    });

Volley Cache

Volley有着强大的缓存机制用来维护请求到的缓存,这节省了不必要的网络消耗和等待时间,下面是一些关于缓存的常用方法

  1. 从缓存中读取请求:即先从缓存读取看是否有缓存数据,如果没有则请求网络数据
Cache cache=ApplicationController.getRequestQueue().getCache();        Entry entry=cache.get(url);        if(entry!=null){            try{                String data=new String(entry.data,"Utf-8");                //处理data,将其转化为JSON,XML,Bitmap等等                }catch(UnspportedEncodingException e){                    e.printStackTrace();                }        }else{            //缓存中不存在,做网络请求        }
  1. 缓存失效:缓存失效并不意味这删除缓存,volley仍将使用缓存对象,直到服务器返回新数据,一旦接收到新数据,将覆盖原来的缓存
ApplicationController.getInstance().getRequestQueue().getCache().invalidate(url,true);
  1. 关闭缓存:如果你想禁用特定Url的缓存可以使用以下方法
StringRequest req=new StringRequest(...);req.setShouldCache(false);
  1. 删除来自特定url的缓存
ApplicationController.getInstance().getRequestQueue().getCache().remove(url);
  1. 删除所有缓存
ApplicationController.getInstance().getRequestQueue().getCache().clear(url);

通过Volley获取数据填充自定义ListView


限于篇幅的问题放到下篇文章中来写->_<

总结:

综上,已经学完了Volley框架的使用,在实际应用中遇到具体的问题需要具体考虑,必要时要学会查阅资料,除了以上几篇提到的参考资料,最好能翻墙去看看google官方关于Volley的文档,我想基本上能解决我们所碰到的问题。

参考资料:Android working with Volley Library


  • 微博: @明桑Android黑历史
  • 邮箱: <13141459344@163.com>
  • 个人主页: 明桑战胜Android汪的黑历史

    微信公众号:ITBird

4 1
原创粉丝点击