Android网络通信必备神器Volley详解——自定义RequestQueue
来源:互联网 发布:手机之间蓝牙共享网络 编辑:程序博客网 时间:2024/06/16 10:01
上一篇文章讲解了如何使用Volley.newRequestQueue来新建一个默认的RequestQueue,本篇文章将要讲解的是新建一个RequestQueue的具体过程,从而可以新建一个自定义的RequestQueue。
设置网络和缓存
RequestQueue需要网络和缓存两部分才能工作。
BasicNetwork
提供基于HTTP的网络传输,DiskBaseCache提供具有内存索引的one-file-per-response缓存。
![](file:///C:\Users\Daniel\AppData\Local\Temp\SGPicFaceTpBq\4160\27B5EB96.gif)
![](file:///C:\Users\Daniel\AppData\Local\Temp\SGPicFaceTpBq\4160\27B5EB96.gif)
设置自定义的RequestQueue
RequestQueue mRequestQueue;// Instantiate the cacheCache 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());<strong>// Instantiate the RequestQueue with the cache and network.mRequestQueue = new RequestQueue(cache, network);</strong>// Start the queuemRequestQueue.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的功能。RequestQueue必须使用Application.contex来初始化,不能使用Activity.contex。这样可以确保RequestQueue在整个app的声明周期中都可以使用。
下面是一个单例模式的例子
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 RequestQueueRequestQueue queue = MySingleton.getInstance(this.getApplicationContext()). getRequestQueue();// ...// Add a request (in this example, called stringRequest) to your RequestQueue.MySingleton.getInstance(this).addToRequestQueue(stringRequest);
1 0
- Android网络通信必备神器Volley详解——自定义RequestQueue
- Android网络通信必备神器Volley详解——初识Volley
- Android网络通信必备神器Volley详解——实现一个自定义的Request(以Gson为例)
- Android网络通信必备神器Volley详解——发送一个标准的Request
- Android--Volley---RequestQueue学习
- Android 网络通信——Volley
- android网络框架volley学习之RequestQueue类
- Android网络框架-Volley(二) RequestQueue源码分析以及建立一个RequestQueue
- Android网络通信框架Volley详解
- Android-网络通信框架Volley使用详解
- Android-网络通信框架Volley使用详解
- Android-网络通信框架Volley使用详解
- Android Volley网络通信详解一
- Android-网络通信框架Volley使用详解
- Android网络通信框架Volley——自定义Request(Get、Post、文件上传)
- Volley RequestQueue
- Android网络通信请求框架——Volley
- Android中关于Volley的使用-RequestQueue
- Android 手动显示和隐藏软键盘
- 添加动作
- 设置UIButton的title 闪动效果的解决办法
- VC新版本编译的程序如何在老系统上运行
- java This的用法
- Android网络通信必备神器Volley详解——自定义RequestQueue
- in_addr 的用法
- XML进阶增删改查及gdv的小知识
- Android 带动画的自定义Toast
- 什么是GP、LP、VC、PE、FOF
- 透明度与十六进制代码转换#ffffffff
- Android基础入门教程——8.3.18 Canvas API详解(Part 3)Matrix和drawBitmapMash
- Command :
- ajax与HTML5 history pushState/replaceState实例