Android开源框架——Volley
Volley 是 Google 在 2013 I/O 大会上推出的 Android 异步网络请求框架和图片加载框架。特别适合数据量小,通信频繁的网络操作。Volley 主要是通过两种 Diapatch Thread 不断从 RequestQueue 中取出请求,根据是否已缓存调用 Cache 或 Network 这两类数据获取接口之一,从内存缓存或是服务器取得请求的数据,然后交由 ResponseDelivery 去做结果分发及回调处理。
原文链接:http://blog.csdn.net/zimo2013/article/details/16971253
Volley特别适合数据量不大但是通信频繁的场景,现在android提供的源码已经包含Volley,以后在项目中,可以根据需求引入Volley jar文件!
2.Volley源码分析
(1).Volley.java
Volley.newRequestQueue()方法在一个app最好执行一次,可以使用单例设计模式或者在application完成初始化,具体原因请查看代码分析
- /**
- * @author zimo2013
- * @see http://blog.csdn.net/zimo2013
- */
- //请求消息队列:RequestQueue;
- public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
- 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) {
- }
A RequestQueue needs two things to do its job: a network to perform transport of the requests(一个网络进行传输的请求), and a cache to handle caching. There are standard implementations of these available in
the Volley toolbox: (有这些可用的标准实现Volley toolbox)DiskBasedCache provides a one-file-per-response cache with an in-memory index,
and BasicNetwork providesa network transport based on your choice of AndroidHttpClient or HttpURLConnection.(DiskBasedCache one-file-per-response缓存提供了一个内存中的指数,并根据您选择BasicNetwork提供了一个网络传输
AndroidHttpClient或HttpURLConnection。) BasicNetwork is Volley‘s default network implementation. A BasicNetwork must be initialized with the HTTP client your app is using to connect to the network. Typically this is AndroidHttpClient or HttpURLConnection:
Use AndroidHttpClient for apps targeting Android API levels lower than API Level 9 (Gingerbread). Prior to Gingerbread, HttpURLConnection was unreliable. For more discussion of this topic, see Android‘s HTTP Clients.Use HttpURLConnection for apps targeting Android API Level 9 (Gingerbread) and higher. To create an app that runs on all versions of Android, you can check the version of Android the device is running and choose the appropriate HTTP client, for example:
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);
- if (stack == null) {
- if (Build.VERSION.SDK_INT >= 9) {
- stack = new HurlStack();
- } else {
- stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
- }
- }
- Network network = new BasicNetwork(stack);
- //cacheDir 缓存路径 /data/data/<pkg name>/cache/<name>
- RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
- queue.start();
- /*
- * 实例化一个RequestQueue,其中start()主要完成相关工作线程的开启,
- * 比如开启缓存线程CacheDispatcher先完成缓存文件的扫描, 还包括开启多个NetworkDispatcher访问网络线程,
- * 该多个网络线程将从 同一个 网络阻塞队列中读取消息
- *
- * 此处可见,start()已经开启,所有我们不用手动的去调用该方法,在start()方法中如果存在工作线程应该首先终止,并重新实例化工作线程并开启
- * 在访问网络很频繁,而又重复调用start(),势必会导致性能的消耗;但是如果在访问网络很少时,调用stop()方法,停止多个线程,然后调用start(),反而又可以提高性能,具体可折中选择
- */
- return queue;
- }
(2).RequestQueue.java
- /**
- * RequestQueue类存在2个非常重要的PriorityBlockingQueue类型的成员字段mCacheQueue mNetworkQueue ,该PriorityBlockingQueue为java1.5并发库提供的新类
- * 其中有几个重要的方法,比如take()为从队列中取得对象,如果队列不存在对象,将会被阻塞,直到队列中存在有对象,类似于Looper.loop()
- *
- * 实例化一个request对象,调用RequestQueue.add(request),该request如果不允许被缓存,将会被添加至mNetworkQueue队列中,待多个NetworkDispatcher线程take()取出对象
- * 如果该request可以被缓存,该request将会被添加至mCacheQueue队列中,待mCacheDispatcher线程从mCacheQueue.take()取出对象,
- * 如果该request在mCache中不存在匹配的缓存时,该request将会被移交添加至mNetworkQueue队列中,待网络访问完成后,将关键头信息添加至mCache缓存中去!
- *
- * @author zimo2013
- * @see http://blog.csdn.net/zimo2013
- */
- public void start() {
- stop();
- 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();
- }
- }
(3).CacheDispatcher.java
- /**
- * @author zimo2013
- * @see http://blog.csdn.net/zimo2013
- */
- @Override
- public void run() {
- Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
- //缓存初始化,会遍历整个缓存文件夹
- mCache.initialize();
- {
- //执行代码
- /*if (!mRootDirectory.exists()) {
- if (!mRootDirectory.mkdirs()) {
- VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
- }
- return;
- }
- File[] files = mRootDirectory.listFiles();
- if (files == null) {
- return;
- }
- for (File file : files) {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
- CacheHeader entry = CacheHeader.readHeader(fis);
- entry.size = file.length();
- putEntry(entry.key, entry);
- } catch (IOException e) {
- if (file != null) {
- file.delete();
- }
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- } catch (IOException ignored) { }
- }
- }*/
- }
- while (true) {
- try {
- //该方法可能会被阻塞
- final Request request = mCacheQueue.take();
- Cache.Entry entry = mCache.get(request.getCacheKey());
- if (entry == null) {
- //缓存不存在,则将该request添加至网络队列中
- mNetworkQueue.put(request);
- continue;
- }
- //是否已经过期
- if (entry.isExpired()) {
- request.setCacheEntry(entry);
- mNetworkQueue.put(request);
- continue;
- }
- Response<?> response = request.parseNetworkResponse(
- new NetworkResponse(entry.data, entry.responseHeaders));
- //存在缓存,执行相关操作
- } catch (InterruptedException e) {
- }
- }
- }
(4).NetworkDispatcher.java
- /**
- * @author zimo2013
- * @see http://blog.csdn.net/zimo2013
- */
- @Override
- public void run() {
- Request request;
- while (true) {
- try {
- //可能会被
- request = mQueue.take();
- } catch (InterruptedException e) {
- // We may have been interrupted because it was time to quit.
- if (mQuit) {
- return;
- }
- continue;
- }
- try {
- //访问网络,得到数据
- NetworkResponse networkResponse = mNetwork.performRequest(request);
- if (networkResponse.notModified && request.hasHadResponseDelivered()) {
- request.finish("not-modified");
- continue;
- }
- // Parse the response here on the worker thread.
- Response<?> response = request.parseNetworkResponse(networkResponse);
- // 写入缓存
- if (request.shouldCache() && response.cacheEntry != null) {
- mCache.put(request.getCacheKey(), response.cacheEntry);
- request.addMarker("network-cache-written");
- }
- // Post the response back.
- request.markDelivered();
- mDelivery.postResponse(request, response);
- } catch (VolleyError volleyError) {
- parseAndDeliverNetworkError(request, volleyError);
- } catch (Exception e) {
- VolleyLog.e(e, "Unhandled exception %s", e.toString());
- mDelivery.postError(request, new VolleyError(e));
- }
- }
- }
(5).StringRequest.java
其中在parseNetworkResponse()中,完成将byte[]到String的转化,可能会出现字符乱码,HttpHeaderParser.parseCharset(response.headers)方法在尚未指定是返回为ISO-8859-1,可以修改为utf-8
- public class StringRequest extends Request<String> {
- private final Listener<String> mListener;
- /**
- * Creates a new request with the given method.
- *
- * @param method the request {@link Method} to use
- * @param url URL to fetch the string at
- * @param listener Listener to receive the String response
- * @param errorListener Error listener, or null to ignore errors
- */
- public StringRequest(int method, String url, Listener<String> listener,
- ErrorListener errorListener) {
- super(method, url, errorListener);
- mListener = listener;
- }
- public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
- this(Method.GET, url, listener, errorListener);
- }
- @Override
- protected void deliverResponse(String response) {
- mListener.onResponse(response);
- }
- @Override
- protected Response<String> parseNetworkResponse(NetworkResponse response) {
- String parsed;
- try {
- //将data字节数据转化为String对象
- parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
- } catch (UnsupportedEncodingException e) {
- parsed = new String(response.data);
- }
- //返回Response对象,其中该对象包含访问相关数据
- return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
- }
- }
(6).ImageLoader.java
- /**
- * @author zimo2013
- * @see http://blog.csdn.net/zimo2013
- */
- public ImageContainer get(String requestUrl, ImageListener imageListener,
- int maxWidth, int maxHeight) {
- throwIfNotOnMainThread();
- final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
- //从mCache得到bitmap对象,因此可以覆写ImageCache,完成图片的三级缓存,即在原有的LruCache添加一个软引用缓存
- Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
- if (cachedBitmap != null) {
- //得到缓存对象
- ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
- imageListener.onResponse(container, true);
- return container;
- }
- ImageContainer imageContainer =
- new ImageContainer(null, requestUrl, cacheKey, imageListener);
- // 首先更新该view,其指定了defaultImage
- imageListener.onResponse(imageContainer, true);
- // 根据可以去检查该请求是否已经发起过
- BatchedImageRequest request = mInFlightRequests.get(cacheKey);
- if (request != null) {
- request.addContainer(imageContainer);
- return imageContainer;
- }
- Request<?> newRequest =
- new ImageRequest(requestUrl, new Listener<Bitmap>() {
- @Override
- public void onResponse(Bitmap response) {
- //如果请求成功
- onGetImageSuccess(cacheKey, response);
- }
- }, maxWidth, maxHeight,
- Config.RGB_565, new ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- onGetImageError(cacheKey, error);
- }
- });
- //添加至请求队列中
- mRequestQueue.add(newRequest);
- //同一添加进map集合,以方便检查该request是否正在请求网络,可以节约资源
- mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer));
- return imageContainer;
- }
- private void onGetImageSuccess(String cacheKey, Bitmap response) {
- //缓存对象
- mCache.putBitmap(cacheKey, response);
- // 请求完成,不需要检测
- BatchedImageRequest request = mInFlightRequests.remove(cacheKey);
- if (request != null) {
- request.mResponseBitmap = response;
- //处理结果
- batchResponse(cacheKey, request);
- }
- }
- private void batchResponse(String cacheKey, BatchedImageRequest request) {
- mBatchedResponses.put(cacheKey, request);
- //通过handler,发送一个操作
- if (mRunnable == null) {
- mRunnable = new Runnable() {
- @Override
- public void run() {
- for (BatchedImageRequest bir : mBatchedResponses.values()) {
- for (ImageContainer container : bir.mContainers) {
- if (container.mListener == null) {
- continue;
- }
- if (bir.getError() == null) {
- container.mBitmap = bir.mResponseBitmap;
- //更新结果
- container.mListener.onResponse(container, false);
- } else {
- container.mListener.onErrorResponse(bir.getError());
- }
- }
- }
- mBatchedResponses.clear();
- mRunnable = null;
- }
- };
- // mHandler对应的looper是MainLooper,因此被MainLooper.loop()得到该message,故该runnable操作在主线程中执行,
- mHandler.postDelayed(mRunnable, mBatchResponseDelayMs);
- }
- }
3.总结
RequestQueue类存在2个非常重要的PriorityBlockingQueue类型的成员字段mCacheQueue mNetworkQueue ,该PriorityBlockingQueue为java1.5并发库提供的!其中有几个重要的方法,比如take()为从队列中取得对象,如果队列不存在对象,将会被阻塞,直到队列中存在有对象,类似于Looper.loop()。实例化一个request对象,调用RequestQueue.add(request),该request如果不允许被缓存,将会被添加至mNetworkQueue队列中,待多个NetworkDispatcher线程从mNetworkQueue中take()取出对象。如果该request可以被缓存,该request将会被添加至mCacheQueue队列中,待mCacheDispatcher线程从mCacheQueue.take()取出对象,如果该request在mCache中不存在匹配的缓存时,该request将会被移交添加至mNetworkQueue队列中,待网络访问完成后,将关键头信息添加至mCache缓存中去,并通过ResponseDelivery主线程调用request的相关方法!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。