Android Volley框架的使用(三)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git
Image Request
为了更方便的使用Volley中的图片请求,我们同样先在VolleyController类中封装一个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);
}
}
package com.javen.volley; import android.content.Context;
import android.text.TextUtils; import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley; public class VolleyController { // 创建一个TAG,方便调试或Log
private static final String TAG = "VolleyController"; // 创建一个全局的请求队列
private RequestQueue reqQueue;
private ImageLoader imageLoader; // 创建一个static ApplicationController对象,便于全局访问
private static VolleyController mInstance; private Context mContext; private VolleyController(Context context) {
mContext=context;
} /**
* 以下为需要我们自己封装的添加请求取消请求等方法
*/ // 用于返回一个VolleyController单例
public static VolleyController getInstance(Context context) {
if (mInstance == null) {
synchronized(VolleyController.class)
{
if (mInstance == null) {
mInstance = new VolleyController(context);
}
}
}
return mInstance;
} // 用于返回全局RequestQueue对象,如果为空则创建它
public RequestQueue getRequestQueue() {
if (reqQueue == null){
synchronized(VolleyController.class)
{
if (reqQueue == null){
reqQueue = Volley.newRequestQueue(mContext);
}
}
}
return reqQueue;
} public ImageLoader getImageLoader(){
getRequestQueue();
//如果imageLoader为空则创建它,第二个参数代表处理图像缓存的类
if(imageLoader==null){
imageLoader=new ImageLoader(reqQueue, new LruBitmapCache());
}
return imageLoader;
} /**
* 将Request对象添加进RequestQueue,由于Request有*StringRequest,JsonObjectResquest...
* 等多种类型,所以需要用到*泛型。同时可将*tag作为可选参数以便标示出每一个不同请求
*/ public <T> void addToRequestQueue(Request<T> req, String tag) {
// 如果tag为空的话,就是用默认TAG
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getRequestQueue().add(req);
} public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
} // 通过各Request对象的Tag属性取消请求
public void cancelPendingRequests(Object tag) {
if (reqQueue != null) {
reqQueue.cancelAll(tag);
}
}
}
- 完成上述步骤后,在使用的时候我们首先需要获取ImageLoader对象
ImageLoader imageLoader=VolleyController.getInstance(context).getImageLoader();
- 将图片载入ImageView 可以使用Volley自己提供的一个Image视图,
NetworkImageView,几行代码就可以搞定//将NetworkImageView布局在布局文件中
NetworkImageView imageView=(NetworkImageView)findViewById(R.id.networkimageview);
//需要用到imageLoader
imageView.setImageUrl(url,imageLoader);如果要将图片直接载入ImageView,可以通过以下方法:
ImageLoader imageLoader=VolleyController.getInstance(context).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){
L.e("Image Error"+error.getMessage());
}
});
Volley Cache
Volley有着强大的缓存机制用来维护请求到的缓存,这节省了不必要的网络消耗和等待时间,下面是一些关于缓存的常用方法
- 从缓存中读取请求:即先从缓存读取看是否有缓存数据,如果没有则请求网络数据
Cache cache=VolleyController.getInstance(context).getRequestQueue().getCache();
Entry entry=cache.get(url);
if(entry!=null){
try{
String data=new String(entry.data,"Utf-8");
//处理data,将其转化为JSON,XML,Bitmap等等
}catch(Exception e){
e.printStackTrace();
}
}else{
//缓存中不存在,做网络请求
} - 缓存失效:缓存失效并不意味这删除缓存,volley仍将使用缓存对象,直到服务器返回新数据,一旦接收到新数据,将覆盖原来的缓存
VolleyController.getInstance(context).getRequestQueue().getCache().invalidate(url,true);
- 关闭缓存:如果你想禁用特定Url的缓存可以使用以下方法
VolleyController.getInstance(context).getRequestQueue().getCache().remove(url);
- 删除来自特定url的缓存
VolleyController.getInstance(context).getRequestQueue().getCache().remove(url);
- 删除所有缓存
VolleyController.getInstance(context).getRequestQueue().getCache()clear(url);
总结:
综上,已经学完了Volley框架的使用,在实际应用中遇到具体的问题需要具体考虑,必要时要学会查阅资料,除了以上几篇提到的参考资料,最好能翻墙去看看google官方关于Volley的文档。
参考资料:Android working with Volley Library blog http://blog.csdn.net/guolin_blog/article/details/17482165
Android Volley框架的使用(三)的更多相关文章
- Android Volley框架的使用(1)
在Android开发中,经常要通过HTTP请求访问网络.为了使通过HTTP请求访问网络的过程更加简单,2013年提出了新的HTTP通信框架--Volley.Volley使用起来非常简单,适用于网络访问 ...
- Android Volley 框架的使用(一)
为什么要使用Volley框架 开发android应用很多时候都要涉及网络操作,Android SDK中提供了HttpClient 和 HttpUrlConnection两种方式用来处理网络操作,但当应 ...
- Android Volley框架的使用(二)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git 使用请求队列RequestQueue Volley中的Request都需要添加到Reque ...
- Android Volley框架的使用(四)图片的三级缓存策略(内存LruCache+磁盘DiskLruCache+网络Volley)
在开发安卓应用中避免不了要使用到网络图片,获取网络图片很简单,但是需要付出一定的代价——流量.对于少数的图片而言问题不大,但如果手机应用中包含大量的图片,这势必会耗费用户的一定流量,如果我们不加以处理 ...
- 035 Android Volley框架进行网络请求
1.volley入门介绍 开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行HTTP通 ...
- Android Volley框架的几种post提交请求方式
首先简单描述一下Google的Android开发团队在2013年推出的一个网络通信框架Volley.它的设计目标是进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如下载文件等,Vol ...
- Android Volley框架的使用(2)
3. 设置请求类型和参数 Volley默认的请求类型是GET,如果需要用POST,可以在构造函数中进行设置.设置参数可以通过重写getParams()方法来实现. private void postR ...
- Android Multimedia框架总结(三)MediaPlayer中创建到setDataSource过程
转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52392430 前言:前一篇的mediaPlayer ...
- Android View框架总结(三)View工作原理
转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52180375 测量/布局/绘制顺序 如何引起View的测量/布局/绘制? Perfor ...
随机推荐
- 线上redis服务内存异常分析。
项目中,新增了一个统计功能,用来统计不同手机型号的每天访问pv,看了下redis2.6有个setbit的功能,于是打算尝尝鲜把 redis从2.4更新到了2.6 因为是租了vps.服务器的内存只有4g ...
- show engine innodb status 详解
找个mysql客户端,执行show engine innodb status得到如下结果: 详细信息如下: ************************************** ======= ...
- SVN中检出(check out) 和 导出(export) 的区别
SVN是常用的一种常见的版本控制软件.SVN中检出(check out) 和 导出(export) 的区别主要有如下几条: check out跟check in对应,export跟import对应. ...
- Textbox像百度一下实现下拉显示 z
List<string> Data = new List<string>(); string Randomstr = "功夫撒黑胡椒hcbvf蜂窝qwertyuiop ...
- .NET下用C#实现邮箱激活功能
最近要用到安全邮箱激活的功能,故写篇博客记录下. 思路:在表中增加一个字段State来记录邮箱是否激活(0激活,1未激活.) 1.发送邮件. 1-1,给邮箱发送邮件.内容:激活地址+GUID. ...
- Android百度地图开发(三)范围搜索
// 1.新建项目 将地图API添加进classpath中: 2.在activity_main.xml中添加一个MapView,用来显示地图: <LinearLayout xmlns:andro ...
- jsoup入门
官网地址:http://jsoup.org/ Jsoup是一个开源的Java库,它可以用于处理实际应用中的HTML.它提供了非常便利的API来进行数据的提取及修改,充分利用了 DOM,CSS以及jqu ...
- 【LeetCode】198 - House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- bzoj 3594 [Scoi2014]方伯伯的玉米田(DP+二维BIT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3594 [题意] 给定一个n个数的序列,有K次将一个区间内的数加1的机会,问最长不下降子 ...
- 30个有关Python的小技巧
从我开始学习python的时候,我就开始自己总结一个python小技巧的集合.后来当我什么时候在Stack Overflow或者在某个开源软件里看到一段很酷代码的时候,我就很惊讶:原来还能这么做!,当 ...