结合开发文档分析volley(二)
来源:互联网 发布:淘宝代码装修教程 编辑:程序博客网 时间:2024/05/19 13:07
这篇笔记记录Setting Up a RequestQueue。设置一个RequestQueue.
创建一个RequestQueue,需要两个东西—-NetWork和Cache。Network做网络有关工作,Cache做缓冲工作。
在Volley有NetWork和Cache的实现——-DiskBasedCache和BasicNetwork。DiskBasedCache保证了一个response对应一个文件,BasicNetwork提供了基于AndroidHttpClient or HttpURLConnection的网络传输。关于BasicNetwork什么时候基于AndroidHttpClient ,什么时候基于HttpURLConnection,下面的代码解释了。
HttpStack stack;...// If the device is running a version >= Gingerbread...if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { // ...use HttpURLConnection for stack.} else { // ...use AndroidHttpClient for stack.}Network network = new BasicNetwork(stack);
android2.3以上(包括2.3)基于HttpURLConnection。
接下来,创建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());// Instantiate the RequestQueue with the cache and network.mRequestQueue = new RequestQueue(cache, network);// Start the queuemRequestQueue.start();
上一篇笔记记录了用 Volley.newRequestQueue创建了一个RequestQueue。看源码。
public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) { File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR); String userAgent = "volley/0"; try { String packageName = context.getPackageName(); PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); userAgent = packageName + "/" + info.versionCode; } catch (NameNotFoundException e) { } if (stack == null) { if (Build.VERSION.SDK_INT >= 9) { stack = new HurlStack(); } else { // Prior to Gingerbread, HttpUrlConnection was unreliable. // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); } } Network network = new BasicNetwork(stack); RequestQueue queue; if (maxDiskCacheBytes <= -1) { // No maximum size specified queue = new RequestQueue(new DiskBasedCache(cacheDir), network); } else { // Disk cache size specified queue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network); } queue.start(); return queue; }
可以看到这方法也是用了NetWork和Cache创建了RequestQueue,然后queue.start(); start()的工作是什么?
public void start() { stop(); // Make sure any currently running dispatchers are stopped. // Create the cache dispatcher and start it. mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery); mCacheDispatcher.start(); // Create network dispatchers (and corresponding threads) up to the pool size. for (int i = 0; i < mDispatchers.length; i++) { NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery); mDispatchers[i] = networkDispatcher; networkDispatcher.start(); } }
看了代码会知道,start是将上一篇笔记提到的CacheDispatcher线程和NetworkDispatcher线程开启。
RequestQueue的单例模式
单例模式是为了使RequestQueue的生命周期和app的一样。
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; }}
0 0
- 结合开发文档分析volley(二)
- 结合开发文档分析volley(一)
- 结合开发文档分析volley(三)
- Volley源码分析二
- Volley(二) 源码分析
- Volley 源码分析(二)
- Volley源码分析(二)
- Volley源码分析(二)-Volley中的Request类
- [Android]Volley源码分析(二)Cache
- [Android]Volley源码分析(二)初始化
- Volley应用与源码分析(二)
- Volley分析
- Volley 分析
- activemq官方文档分析二
- Android Volley分析(二)——实现
- Volley简单学习使用四——源码分析二
- Android Volley源码分析(二) 核心功能解析
- 【Volley核心类分析】CacheDispatcher、NetworkDispatcher以及ResponseDelivery(二)
- 理解js中原型
- 动态规划 III——A - Railway tickets
- 修改grub2默认启动顺序
- c++中vector的用法详解
- Power of Three
- 结合开发文档分析volley(二)
- SG函数
- Java单例模式与多线程
- 读书笔记-陆-《从你的全世界路过》
- 码神作业(九九乘法表)
- Android 6.0特性
- 递归
- Partition List
- ZYNQ Z-TURN BOARD 学习笔记1-Ubuntu上的流水灯实验