Volley是Android系统下的一个网络通信库。为Android提供简单高速的网络操作(Volley:Esay, Fast Networking for Android),以下是它的结构:

既然是网络通信库,自然会涉及到网络的基础操作:请求和响应。也是最主要的概念。client发出请求。服务端返回响应的字节数据。client解析得到想要的结果。Volley怎么设计这些主要的概念?

一、组件

1、Network

网络操作的定义,传入请求Request,得到响应NetworkResponse

public interface Network {
/**
* Performs the specified request.
* @param request Request to process
* @return A {@link NetworkResponse} with data and caching metadata; will never be null
* @throws VolleyError on errors
*/
public NetworkResponse performRequest(Request<? > request) throws VolleyError;
}

2、Request

请求的定义,包括网络请求的參数、地址等信息

public Request(int method, String url, Response.ErrorListener listener) {
mMethod = method;
mUrl = url;
mErrorListener = listener;
setRetryPolicy(new DefaultRetryPolicy()); mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
}

在Request中有两个抽象方法须要子类去实现。

/**
* Subclasses must implement this to parse the raw network response
* and return an appropriate response type. This method will be
* called from a worker thread. The response will not be delivered
* if you return null.
* @param response Response from the network
* @return The parsed response, or null in the case of an error
*/
abstract protected Response<T> parseNetworkResponse(NetworkResponse response); /**
* Subclasses must implement this to perform delivery of the parsed
* response to their listeners. The given response is guaranteed to
* be non-null; responses that fail to parse are not delivered.
* @param response The parsed response returned by
* {@link #parseNetworkResponse(NetworkResponse)}
*/
abstract protected void deliverResponse(T response);

一个是 parseNetworkResponse。就是说对于返回的数据。须要怎么去解析。解析成什么类型的数据。一个详细的请求。应该知道自己想要什么结果。比方StringRequest就是将结果解析成String。而ImageRequest则是将结果解析成Bitmap。这里作为抽象方法留给详细的子类实现;

还有一个是 deliverResponse。用于解析完毕后将结果传递出去。这里传入的是解析好的数据类型,通常会在里面通过listener将结果传递到应用的场景下。如StringRequest,

    @Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}

3、NetworkResponse

网络请求通用返回结果。数据存储在data中

    public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
boolean notModified) {
this.statusCode = statusCode;
this.data = data;
this.headers = headers;
this.notModified = notModified;
}

4、Response<T>

响应结果的封装。包括终于结果result。缓存结构cacheEntry。出错信息error

private Response(T result, Cache.Entry cacheEntry) {
this.result = result;
this.cacheEntry = cacheEntry;
this.error = null;
}

二、运行过程

有了上面的基本数据结构,之后就是考虑怎么去操作这些结构。完毕从请求到响应的整个过程。

这里是整个Volley最核心的部分。也是体现作者设计思想的部分。涉及到任务调度、异步处理等。

1、RequestQueue

请求队列。全部请求都会通过RequestQueue的add方法增加到内部的队列里来等待处理,当请求结束得到响应结果后会调用finish方法将请求移出请求队列。

RequestQueue中包括下面成员:

  • Cache 缓存结构,用于缓存响应结果;
  • Network 网络操作的实现
  • NetworkDispatcher 网络任务调度器
  • CacheDispatcher 缓存任务调度器
  • ResponseDelivery 响应投递,用于将结果从工作线程转移到UI线程
  • cacheQueue 缓存任务队列
  • networkQueue 网络任务队列

RequestQueue完毕两项工作:

启动、停止调度器:

/**
* Starts the dispatchers in this queue.
*/
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();
}
} /**
* Stops the cache and network dispatchers.
*/
public void stop() {
if (mCacheDispatcher != null) {
mCacheDispatcher.quit();
}
for (int i = 0; i < mDispatchers.length; i++) {
if (mDispatchers[i] != null) {
mDispatchers[i].quit();
}
}
}

将请求加入到对应的队列中,之后各个调度器会自行取出处理

    public <T> Request<T> add(Request<T> request) {
// Tag the request as belonging to this queue and add it to the set of current requests.
request.setRequestQueue(this);
synchronized (mCurrentRequests) {
mCurrentRequests.add(request);
} // Process requests in the order they are added.
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue"); // If the request is uncacheable, skip the cache queue and go straight to the network.
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
} // Insert request into stage if there's already a request with the same cache key in flight.
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new LinkedList<Request<?>>();
}
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
} else {
// Insert 'null' queue for this cacheKey, indicating there is now a request in
// flight.
mWaitingRequests.put(cacheKey, null);
mCacheQueue.add(request);
}
return request;
}
}

从上面能够看出,一个请求不要求缓存的话会被直接加到networkQueue中。否则会加到cacheQueue中。

那调度器是怎么自行取出来并进行处理呢?

2、CacheDispatcher,NetworkDispatcher

调度器是线程,队列堵塞。有请求就运行。没有就等待。

对缓存调度器CacheDispatcher,假设在缓存中没有找到响应结果。就会将请求加入到网络调度器NetworkDispatcher中

while (true) {
try {
// Get a request from the cache triage queue, blocking until
// at least one is available.
final Request<? > request = mCacheQueue.take();
request.addMarker("cache-queue-take"); // If the request has been canceled, don't bother dispatching it.
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
} // Attempt to retrieve this item from cache.
Cache.Entry entry = mCache.get(request.getCacheKey());
if (entry == null) {
request.addMarker("cache-miss");
// Cache miss; send off to the network dispatcher.
mNetworkQueue.put(request);
continue;
} // If it is completely expired, just send it to the network.
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
} // We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed"); if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry); // Mark the response as intermediate.
response.intermediate = true; // Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}

NetworkDispatcher则是负责运行网络操作获取响应,调用Request解析响应从而得到指定的返回数据类型,将结果增加缓存。使用delivery将结果返回到UI线程。

Android Volley分析(一)——结构的更多相关文章

  1. Android Volley源码分析

    今天来顺手分析一下谷歌的volley http通信框架.首先从github上 下载volley的源码, 然后新建你自己的工程以后 选择import module 然后选择volley. 最后还需要更改 ...

  2. [Android]Volley源代码分析(二)Cache

    Cache作为Volley最为核心的一部分,Volley花了重彩来实现它.本章我们顺着Volley的源代码思路往下,来看下Volley对Cache的处理逻辑. 我们回忆一下昨天的简单代码,我们的入口是 ...

  3. Android多线程分析之四:MessageQueue的实现

    Android多线程分析之四:MessageQueue的实现 罗朝辉 (http://www.cnblogs.com/kesalin/) CC 许可,转载请注明出处 在前面两篇文章<Androi ...

  4. [Android]Volley的使用

    Volley是Google I/O 2013上提出来的为Android提供简单快速网络访问的项目.Volley特别适合数据量不大但是通信频繁的场景. 优势 相比其他网络载入类库,Volley 的优势官 ...

  5. Android Volley入门到精通:定制自己的Request

    经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...

  6. Android Volley完全解析

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

  7. Android开发之 Android应用程序目录结构解析

    建立的HelloWorld的应用项目,其代码是由ADT插件自动生成的,形成Android项目特有的结构框架. 接下来让我带领大家解析一个Android程序的各个组成部分,这次我们拿一个Hello,Wo ...

  8. Android 核心分析 之六 IPC框架分析 Binder,Service,Service manager

    IPC框架分析 Binder,Service,Service manager 我首先从宏观的角度观察Binder,Service,Service Manager,并阐述各自的概念.从Linux的概念空 ...

  9. Android架构分析之使用自定义硬件抽象层(HAL)模块

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android版本:2.3.7_r1 Linux内核版本:android-goldfish-2.6.29 在上一篇博 ...

随机推荐

  1. 大数相减 C语言

    #include <stdio.h> #include <string.h> using namespace std; ],b[]; void Sub() { ; if(a = ...

  2. 九度oj 题目1348:数组中的逆序对

    题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 输入: 每个测试案例包括两行: 第一行包含一个整数n,表示数组 ...

  3. R语言处理1975-2011年的人口信息

    1975-2011年的数据中. 1)分别统计每年人口最多的国家是哪个?有多少 2)统计出各个国家的1975-2011年的平均人口增长率 3)统计每年人口最多的十个国家 4)统计出每年人口最少的十个国家 ...

  4. 用JS实现倒计时(日期字符串作为参数)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】

    题目 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱子拿到礼物,升职 ...

  6. django无法同步数据库 Error loading MySQLdb module: No module named ‘MySQLdb‘

    最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...

  7. Enable and Use Remote Commands in Windows PowerShell

    The Windows PowerShell remoting features are supported by the WS-Management protocol and the Windows ...

  8. for循环创建对象

    有时候奇怪的发现往list添加数据的时候,一直被最后一个元素覆盖,首先 ,我们得明白原理: 在new 一个对象的时候,对象的ID是唯一确定的:将对象add入list中时,放入list中的其实是对象的引 ...

  9. php--strlen()与mb_strlen的作用与区别

    在PHP中,strlen与mb_strlen是求字符串长度的函数PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得 ...

  10. POJ 3368.Frequent values-处理数据+RMQ(ST)

    昨天写的博客删了,占坑失败,还是先把RMQ玩的6一点再去搞后面的东西.废话少说,题解题姐姐_(:з」∠)_      Frequent values Time Limit: 2000MS   Memo ...