Volley提供2个静态方法:

public static RequestQueue newRequestQueue(Context context) {}

public static RequestQueue newRequestQueue(Context context, HttpStack stack) {} 
第一个直接调用第二个的newRequestQueue(context, null);方法,返回都是一个RequestQueue 对象
以ImageLoader为例 构造方法
 
public ImageLoader(RequestQueue queue, ImageCache imageCache) {
mRequestQueue = queue;
mCache = imageCache;
}
 
需要一个RequestQueue 对象 和一个ImageCache对象 ,RequestQueue 可以通过Volley类中的静态方法newRequestQueue(Context context) 获得,而ImageCache 则是一个interface
 public interface ImageCache {
public Bitmap getBitmap(String url);
public void putBitmap(String url, Bitmap bitmap);
}
实现此接口,即上一文章中Cache包下的BitmapCache,赋值给mCache对象
mCache中缓存key value,key规则为String key = "#W"+maxWidth+"#H"+ maxHeight +url;
再来看下面方法
参数:defaultImageResId:未开始下载显示的图片Id,如果传递0则不显示任何东西
           errorImageResId:下载显示的图片Id,如果传递0则不显示任何东西
返回值:ImageListener
 
public static ImageListener getImageListener(final ImageView view,
final int defaultImageResId, final int errorImageResId) {
...
...//直接new一个ImageListener
@Override public void onResponse(ImageContainer response, boolean isImmediate)
{ if (response.getBitmap() != null)
{ //response有值设置ImageView显示图片
view.setImageBitmap(response.getBitmap()); } else if (defaultImageResId != ) { view.setImageResource(defaultImageResId); } } }
response.getBitmap()中的bitmap是通过BatchedImageRequest中的batchResponse(),
通过一个ImageContainer类中的Interface:ImageListerner.onResponse回调回来的
 
private void batchResponse(String cacheKey, BatchedImageRequest request,
final VolleyError error) {
mBatchedResponses.put(cacheKey, request);
// If we don't already have a batch delivery runnable in flight, make a new one.
// Note that this will be used to deliver responses to all callers in mBatchedResponses.
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 (error == null) {
//取得数据
container.mBitmap = bir.mResponseBitmap;
//通过ImageContainer中的mListener,即ImageListener中的onResponse()方法回调
container.mListener.onResponse(container, false);
} else {
container.mListener.onErrorResponse(error);
}
}
}
mBatchedResponses.clear();
mRunnable = null;
} };
// Post the runnable.
mHandler.postDelayed(mRunnable, mBatchResponseDelayMs);
}
}

Android 网络通信框架Volley(二)的更多相关文章

  1. Android 网络通信框架Volley简介(Google IO 2013)

    1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient( ...

  2. [转]Android 网络通信框架Volley简介(Google IO 2013)

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  3. 【转】Android 网络通信框架Volley简介(Google IO 2013)

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  4. Android 网络通信框架Volley(一)

    转自:http://blog.csdn.net/t12x3456/article/details/9221611 1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫 ...

  5. Android 网络通信框架Volley简介

    1.1. Volley引入的背景在以前,我们可能面临如下很多麻烦的问题. 比如以前从网上下载图片的步骤可能是这样的流程: 在ListAdapter#getView()里开始图像的读取. 通过Async ...

  6. Android 网络通信框架Volley的简单使用

    Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮. Volley提供的功能: JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的优先级处理 ...

  7. Android 网络通信框架Volley基本介绍

    Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...

  8. Android 网络通信框架Volley(三)

    NetworkImageView 分析:public class NetworkImageView extends ImageView 他继承自ImageView,并且添加了一个新方法: public ...

  9. Android网络框架-Volley实践 使用Volley打造自己定义ListView

    这篇文章翻译自Ravi Tamada博客中的Android Custom ListView with Image and Text using Volley 终于效果 这个ListView呈现了一些影 ...

随机推荐

  1. 使用JMS接口接入WebSphere MQ消息

    在你的应用程序中利用IBM WebSphere MQ消息中间件提供Java消息服务开放接口. IBM WebSphere MQ(WMQ)是一套面向消息的中间件(message-oriented mid ...

  2. 工作中常见的五种技术leader

    力不从心型 在工作中有种技术leader,总认为自己是最好的.在方案设计的时候,自己有一种方案,下属有一种方案.leader非要别人听他的.如果两种方案没有优劣之分,比较建议的做法是让真正实施的人按照 ...

  3. SQL中一些实用的快捷键

    Ctrl+A全选 快速选中一行: 若光标在这条语句末尾用Shift+Home 若光标在这条语句开头用Shift+End Ctrl+K+U快捷注释本行 Ctrl+K+C反注释 Ctrl+R  关闭下面的 ...

  4. STL 队列

    头文件 #include <queue> 定义 普通队列: queue < int > q; 优先队列: priority_queue < int, vector< ...

  5. Fabric项目学习总结

    1.Hyperledger Fabric的基本架构 2.PKI机制

  6. maven阿里云镜像setting

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  7. JDK基础必备面试十问

    1. new一个对象在Java内部做了哪些工作? 从静态角度来看,new一个对象表示创建一个类的对象实例. 从JVM运行角度来看,当JVM执行到new字节码时,首先会去查看类有没有被加载到内存以及初始 ...

  8. ORACLE中添加删除主键

    本文转自:http://blog.chinaunix.net/uid-17079336-id-2832443.html 1.创建表的同时创建主键约束(1)无命名create table student ...

  9. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  10. 【原创】Linux cpuidle framework

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...