盒子
盒子
文章目录
  1. newRequestQueue
  2. RequestQueue
    1. start
    2. add
    3. finish
  3. CacheDispatcher
    1. run
    2. ExecutorDelivery
      1. postResponse
      2. ResponseDeliveryRunnable
  4. NetworkDispatcher
    1. run
  5. 总结

Android Volley源码分析(一)

volley是一个非常流行的Android开源框架,自己平时也经常使用它,但自己对于它的内部的实现过程并没有进行太多的深究。所以为了以后能更通透的使用它,了解它的实现是一个非常重要的过程。自己有了一点研究,做个笔记同时与大家一起分享。期间自己也画了一张图,希望能更好的帮助我们理解其中的步骤与原理。如下:
Volley框架图
开始看可能会一脸懵逼,我们先结合源码一步一步来,现在让我们一起进入Volley的源码世界,来解读大神们的编程思想。

newRequestQueue

如果使用过Volley的都知道,不管是进行网络请求还是什么别的图片加载,首先都要创建好一个RequestQueue

1
public static final RequestQueue volleyQueue = Volley.newRequestQueue(App.mContext);

RequestQueue的创建自然离不开Volley中的静态方法newRequestQueue,从上面的图片也能知道首先进入点是newRequestQueue,好了现在我们来看下该方法中到底做了什么:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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) {
}
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 = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}

从上面的源码中我们能发现有一个stack,它代表着网络请求通信HttpClientHttpUrlConnection,但我们一般都是默认设置为null。因为它会默认帮我们进判断选择更适合的。当手机版本大于等于9时会使用HurlStack它里面使用HttpUrlConnection进行实现通信的,而小于9时则创建HttpClientStack,它里面自然是HttpClient的实现。通过BasicNetwork构造成Network来进行传递使用;在这之后构建了RequestQueue,其中帮我们构造了一个缓存new DiskBasedCache(cacheDir),默认为5MB,缓存目录为volley。所以我们能在data/data/应用包名/volley下找到缓存。最后调用其start方法,并返回RequestQueue。这就是我们前面第一段代码的内部实现。下面我们进入RequestQueue中的start方法看下它到底做了什么呢?

RequestQueue

RequestQueue中通过this调用自身来默认帮我们调用了下面的构造函数

1
2
3
4
5
6
7
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}

cache后续在NetworkDispatcher中会帮我们进行response.cacheEntry的缓存,netWork是前面的根据版本所封装的通信,threadPoolSize线程池大小默认为4delivery,是ExecutorDelivery作用在主线程,在最后对请求响应的分发。

start

1
2
3
4
5
6
7
8
9
10
11
12
13
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方法中我们发现它实现了两个dispatch,一个是CacheDispatcher缓存派遣,另一个是networkDispatcher进行网络派遣,其实他们都是Thread,所以都调用了他们的start方法。其中networkDispatcher默认构建了4个,相当于包含4个线程的线程池。现在我们先不去看他们内部的run方法到底实现了什么,我们还是接着看RequestQueue中我们频繁使用的add方法。

add

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
}
}

我们结合代码与前面的图,首先会为当前的request设置RequestQueue,并且根据情况同步是否是当前正在进行的请求,加入到mCurrentRequests中,看11行代码,之后对当前request进行判断是否需要缓存(默认实现是true,如果不需要可调用request.setShouldCache()进行设置),如果不需要则直接加入到前面的mNetworkQueue中,它会在CacheDispatcherNetworkDispatcher中做相应的处理,然后返回request。如果需要缓存,看16行代码,则对mWaitingRequests中是否包含cacheKey进行相应的处理。其中cacheKey为请求的url。最后再加入到缓存队列mCacheQueue中。

finish

细心的人会发现当对应cacheKeyvalue不为空时,创建了LinkedListQueue,只是将request加入到了Queue中,只是更新了mWaitingRequests中相应的value但并没有加入到mCacheQueue中。其实不然,因为后续会调用finish方法,我们来看下源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<T> void finish(Request<T> request) {
// Remove from the set of requests currently being processed.
synchronized (mCurrentRequests) {
mCurrentRequests.remove(request);
}
synchronized (mFinishedListeners) {
for (RequestFinishedListener<T> listener : mFinishedListeners) {
listener.onRequestFinished(request);
}
}
if (request.shouldCache()) {
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
if (waitingRequests != null) {
if (VolleyLog.DEBUG) {
VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.",
waitingRequests.size(), cacheKey);
}
// Process all queued up requests. They won't be considered as in flight, but
// that's not a problem as the cache has been primed by 'request'.
mCacheQueue.addAll(waitingRequests);
}
}
}
}

1422行代码,正如上面我所说,会将Queue中的request全部加入到mCacheQueue中。
好了RequestQueue的主要源码差不多就这些,下面我们进入CacheDispatcher的源码分析,看它究竟如何工作的呢?

CacheDispatcher

前面提到了它与NetworkDispatcher本质都是Thread,那么我们自然是看run方法

run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

// Make a blocking call to initialize the cache.
mCache.initialize();

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.
}
}
});
}

} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
}
}

看起来很多,我们结合图来挑主要的看。首先创建了一个无限循环一直在监视着request的变化。从缓存队列mCacheQueue中获取request,如果该请求是cancle了,调用request.finish()清除相应数据并进行下一个请求的操作,否则从前面提到的mCache中获取Cache.Entry。如果不存在或者已经过期,将请求加入到网络队列中mNetWorkQueue,进行后续的网络请求。如果存在(41行代码)则进行request.parseNetworkResponse()解析出response,不同的request对应不同的解析方法。例如StringRequestJsonObjectRequest有各自的解析实现。再看45行,发现不管entry是否需要更新的,都会进一步对response进行mDelivery.postResponse(request, response)递送,不同的是需要更新的话重新设置requestentry与加入到mNetworkQueue中,也就相当与重新进行网络请求一遍。那么再回到递送的阶段,前面已经提到在创建RequestQueue是实现了ExecutorDelivery,mDelivery.postResponse就是其中的方法。我们来看一下

ExecutorDelivery

在这里创建了一个Executor,对后面进行递送,作用在主线程

1
2
3
4
5
6
7
8
9
public ExecutorDelivery(final Handler handler) {
// Make an Executor that just wraps the handler.
mResponsePoster = new Executor() {
@Override
public void execute(Runnable command) {
handler.post(command);
}
};
}

postResponse

1
2
3
4
5
6
7
8
9
10
11
@Override
public void postResponse(Request<?> request, Response<?> response) {
postResponse(request, response, null);
}

@Override
public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
request.markDelivered();
request.addMarker("post-response");
mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
}

这个方法就简单了就是调用execute进行执行,在进入ResponseDeliveryRunnablerun看它如何执行

ResponseDeliveryRunnable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void run() {
// If this request has canceled, finish it and don't deliver.
if (mRequest.isCanceled()) {
mRequest.finish("canceled-at-delivery");
return;
}

// Deliver a normal response or error, depending.
if (mResponse.isSuccess()) {
mRequest.deliverResponse(mResponse.result);
} else {
mRequest.deliverError(mResponse.error);
}

// If this is an intermediate response, add a marker, otherwise we're done
// and the request can be finished.
if (mResponse.intermediate) {
mRequest.addMarker("intermediate-response");
} else {
mRequest.finish("done");
}

// If we have been provided a post-delivery runnable, run it.
if (mRunnable != null) {
mRunnable.run();
}
}

主要是第9行代码,对于不同的响应做不同的递送,deliverResponsedeliverError内部分别调用的就是我们非常熟悉的Listener中的onResponseonErrorResponse方法,进而返回到我们对网络请求结果的处理函数。

这就是整个的缓存派遣,简而言之,存在请求响应的缓存数据就不进行网络请求,直接调用缓存中的数据进行分发递送。反之执行网络请求。
下面我来看下NetworkDispatcher是如何处理的

NetworkDispatcher

run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) {
long startTimeMs = SystemClock.elapsedRealtime();
Request<?> request;
try {
// Take a request from the queue.
request = mQueue.take();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}

try {
request.addMarker("network-queue-take");

// If the request was cancelled already, do not perform the
// network request.
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}

addTrafficStatsTag(request);

// Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");

// If the server returned 304 AND we delivered a response already,
// we're done -- don't deliver a second identical response.
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
continue;
}

// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");

// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.
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) {
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
VolleyError volleyError = new VolleyError(e);
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
mDelivery.postError(request, volleyError);
}
}
}

在这里也创建了一个无限循环的while,同样也是先获取request,不过是从mQueue中,即前面多次出现的mNetWorkQueue,通过看代码发现一些实现跟CacheDispatcher中的类似。也正如图片中所展示的一样,(23行)如何请求取消了,直接finish;否则进行网络请求,调用(31行)mNetwork.performRequest(request),这里的mNetWork即为前面RequestQueue中对不同版本进行选择的stack的封装,分别调用HurlStackHttpClientStack各自的performRequest方法,该方法中构造请求头与参数分别使用HttpClient或者HttpUrlConnection进行网络请求。我们再来看42行,是不是很熟悉,与CacheDispatcher中的一样进行response进行解析,然后如果需要缓存就加入到缓存中,最后(54行)再调用mDelivery.postResponse(request, response)进行递送。至于后面的剩余的步骤与CacheDispatcher中的一模一样,这里就不多累赘了。

好了,Volley的源码解析先就到这里了,我们再回过去看那张图是不是感觉很清晰了呢?

总结

我们来对使用Volley网络请求做个总结

  • 通过newRequestQueue初始化与构造RequestQueue
  • 调用RequestQueue中的add方法添加request到请求队列中
  • 缓存派遣,先进行CacheDispatcher,判断缓存中是否存在,有则解析response,再直接postResponse递送,否则进行后续的网络请求
  • 网络派遣,NetworkDispatcher中进行相应的request请求,解析response如设置了缓存就将结果保存到cache中,再进行最后的postResponse递送。
支持一下
赞赏是一门艺术