Volley使用指南第二回(来自developer.android)
上一篇文章翻译了一下google的Volley官方文档,讲到了最基本的发送request。这一次我们来下一回:创建一个自定义RequestQueue。
这篇文章将会教你一步一步创建自己的RequestQueue,并定制你自己的行为。
第一步:使用定制的Network和cache
一个RequestQueue需要两样东西去完成他的工作:即发送请求的network和处理缓存的cache。在Volley中有两个实现的标准接口:那么DiskBasedCache提供一个
一个response对应一个文件的缓存和内存级别的索引。BasicNetwork提供基于Http client的网络传输。
BasicNetwork是Volley默认网络的网络接口,它必须被一个你正在使用的Http client初始化才能使用。就像下面这样:
RequestQueue mRequestQueue; // Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap // Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack()); // Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network); // Start the queue
mRequestQueue.start(); String url ="http://www.example.com"; // Formulate the request and handle the response.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
}); // Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);
如果你只是一次性请求并且想要使用线程池,你可以在任意你想要的时候创建RequestQueue,然后再数据返回或者出错后调用stop()方法。但是更常用的方式是以单例的
方式去创建RequestQueue保证它运行在你的应用的生命周期内。
第二步:使用单例模式
如果你的应用持续使用网络,那么最好使用单例模式。实现它有很多种方式,推荐使用实现一个封装好RequestQueue和Volley其他功能的类。另一种方法是继承Application
并且在你的Application.onCreate()方法中创建RequestQueue ,但是这种方法是不被推荐的。一个单例可以实现同样的功能并且更加符合模块化的设计。
重要的一点是RequestQueue必须使用Application context而不是一个Activity Context。这可以保证RequestQueue在app的整个生命周期内都有效,而不是每次重新创建
Activity都要重新创建(比如手机从横屏到竖屏)。
下面是一个栗子:
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
// Get a RequestQueue
RequestQueue queue = MySingleton.getInstance(this.getApplicationContext()).
getRequestQueue(); // ... // Add a request (in this example, called stringRequest) to your RequestQueue.
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
Volley使用指南第二回(来自developer.android)的更多相关文章
- Volley使用指南第三回(来自developer.android)
继第二篇之后,再来Volley使用的教程的第三篇,有些翻译我是根据自己的理解,可能有错误的地方,还请多多包涵. 标准请求 这一回课将会告诉你Volley能够完成的3种请求类型 1.StringReqe ...
- Volley使用指南第一回(来自developer.android)
最近闲来想看看android网络方面的东西.google在2013年发布了一个叫做Volley的网络请求框架,我看了一下官网,居然在training里面就有教程.首先,英文的东西看着 还是挺不爽的,特 ...
- Volley使用指南第四回(来自developer.android)
Volley网络请求的第四篇,废话不多说,开始. 这一篇文章将会教你怎样在Volley支持的范围内定制一个请求. 第一步:写一个通用请求: 大多数请求都有已经写好的接口供你调用,如果你的请求是Stri ...
- 第三部分:Android 应用程序接口指南---第二节:UI---第十一章 样式和主题
第11章 样式和主题 style是用于指定View或window的外观和格式的一系列属性的集合.style可以指定高(height).填补(padding).字体颜色.字体大小.背景颜色等等属性.st ...
- [翻译]现代java开发指南 第二部分
现代java开发指南 第二部分 第二部分:部署.监控 & 管理,性能分析和基准测试 第一部分,第二部分 =================== 欢迎来到现代 Java 开发指南第二部分.在第一 ...
- 对怎样充分利用安卓官方开发网站的一个简单性介绍介绍-https://developer.android.google.cn/docs/
一,谷歌的安卓官方网站-https://developer.android.google.cn/docs/ ,在网站里面可以查询开发文档,开发指导,设计原则,制作app的例子等等,无论对于新手还是老手 ...
- (备忘)Nginx中文手册(技术指南第二版)
Nginx 常见应用技术指南[Nginx Tips] 第二版 目 录 一. Nginx 基础知识二. Nginx 安装及调试三. Nginx Rewrite四. Nginx Redirect五. Ng ...
- CSS Transform完全指南(第二版) #flight.Archives007
Title/ CSS Transform完全指南(第二版) #flight.Archives007 序: 第7天了! 终身学习, 坚持创作, 为生活埋下微小的信仰. 我是忘我思考,共同进步! 简介: ...
- Knockout应用开发指南 第二章:监控属性(Observables)
原文:Knockout应用开发指南 第二章:监控属性(Observables) 关于Knockout的3个重要概念(Observables,DependentObservables,Observabl ...
随机推荐
- 手动配置gradle
最近从github倒入项目,运行的特别慢gradle配置有问题,解决方法: 1.C:\android\demo\hellocharts-android-master\gradle\wrapper 目录 ...
- [转]Jquery Ajax用法
原文地址:http://www.php100.com/html/program/jquery/2013/0905/6004.html jQuery学习之jQuery Ajax用法详解 来源: 时间 ...
- javascript OOP编辑思想的一个实践参考
<html> <style type="text/css"> .current { background-color: red; } .dv { backg ...
- 【C#学习笔记】退出程序
1.this.Close(); 只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit(); 强制所有消息中 ...
- Android 使用库项目时的一个特殊tip
前提: 项目A作为库项目被项目B引用,但是项目A中有自定义的控件和自定义的属性,当在项目B中使用自定义的属性时,编译时就会直接报错:No resource identifier found for a ...
- Blog CSS
你好 print("你好.") haode
- 安装--SambaServce
参考地址:快跑蚂蚁的linux之旅--redhat安装配置samba实验win共享linux主机目录 1.使用rpm -qa|grep "samba",查看samba安装包是否安装 ...
- erp验收测试
软件测试是为了发现错误而执行程序的过程.它不仅是软件开发阶段的有机组成部分,而且在整个软件工程(即软件定义.设计和开发过程)中占据相当大的比重.软件测试是软件质量保证的关键环节,直接影响着软件的质量评 ...
- Python 笔记 : 类和继承
# -*- coding= utf-8 -*- # 文件编码定义的语法规则是: coding[:=]/s*([-/w.]+) # 未指定编码将默认为 : ASCII # 同时要注意物理文件的编码也要 ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...