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. 给debian的docker容器添加crontab定时任务

    现在大部分的docke镜像是基于debian # cat /etc/issue Debian GNU/Linux 9 \n \l Docker容器是不支持后台服务的,像systemctl servic ...

  2. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. Oracle中的通用函数

    1.nvl(列,默认值)函数处理null select nvl(null,3),nvl(4,3) from dual    结果显示为3,4.因为nvl中的第一个为null时,返回结果为第二个值,第一 ...

  4. JavaScript数组方法大全(第二篇)

    数组方法大全(第二篇) 注意:如有错误欢迎指出,如有雷同纯属巧合,本博客参考书籍JavaScript权威指南,有兴趣的小伙伴可以去翻阅一下哦 forEach()方法 遍历数组,里面可以传递一个方法 v ...

  5. 从一道没人能答对的面试题聊聊Java的值传递

    这是一道我们公司的面试题,从招第二个Java以来就一直存在了.但是面试了这么长的时间还没有一个人可以全部答对,让我们一度以为是这题出的不对.首先请看面试题. 以下运算的输出分别是多少: ```java ...

  6. Flink中watermark为什么选择最小一条(源码分析)

    昨天在社区群看到有人问,为什么水印取最小的一条?这里分享一下自己的理解 首先水印一般是设置为:(事件时间 - 指定的值)  这里的作用是解决迟到数据的问题,从源码来看一下它如何解决的 先来看下wind ...

  7. Keras实例教程(3)

    https://blog.csdn.net/baimafujinji/article/details/80705578

  8. Git随身手册

    Git随身手册 本文是关于Git探索的一篇文章,阐述了Git的大部分命令和使用方式,并列举了几个典型的使用场景以供参考和体会. 对于Git这个分布式的VCS,从链表的角度来看待是最容易理解的: 一次c ...

  9. vue-cli+vue 2.0+element-ui+vue-router+echarts.js开发后台管理系统项目教程

    一.首先使用npm创建vue项目框架: 1.安装vue-cli:    $ npm install --global vue-cli 2.初始化项目:$ npm init webpack  项目名 3 ...

  10. C#ORM中的对象映射

    使用Linq.Expressions来动态生成映射方法 1.我们先写个简单的类Test,包含一个ID和Name. public class Test { public int? ID { get; s ...