volley
是一个非常流行的Android
开源框架,自己平时也经常使用它,但自己对于它的内部的实现过程并没有进行太多的深究。所以为了以后能更通透的使用它,了解它的实现是一个非常重要的过程。自己有了一点研究,做个笔记同时与大家一起分享。期间自己也画了一张图,希望能更好的帮助我们理解其中的步骤与原理。如下:
开始看可能会一脸懵逼,我们先结合源码一步一步来,现在让我们一起进入Volley
的源码世界,来解读大神们的编程思想。
newRequestQueue
如果使用过Volley
的都知道,不管是进行网络请求还是什么别的图片加载,首先都要创建好一个RequestQueue
public static final RequestQueue volleyQueue = Volley.newRequestQueue(App.mContext);
而RequestQueue
的创建自然离不开Volley
中的静态方法newRequestQueue
,从上面的图片也能知道首先进入点是newRequestQueue
,好了现在我们来看下该方法中到底做了什么:
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
,它代表着网络请求通信HttpClient
与HttpUrlConnection
,但我们一般都是默认设置为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
调用自身来默认帮我们调用了下面的构造函数
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
线程池大小默认为4
,delivery
,是ExecutorDelivery
作用在主线程,在最后对请求响应的分发。
start
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
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
中,它会在CacheDispatcher
与NetworkDispatcher
中做相应的处理,然后返回request
。如果需要缓存,看16行代码,则对mWaitingRequests
中是否包含cacheKey
进行相应的处理。其中cacheKey
为请求的url
。最后再加入到缓存队列mCacheQueue
中。
finish
细心的人会发现当对应cacheKey
的value
不为空时,创建了LinkedList
即Queue
,只是将request
加入到了Queue
中,只是更新了mWaitingRequests
中相应的value
但并没有加入到mCacheQueue
中。其实不然,因为后续会调用finish
方法,我们来看下源码:
<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);
}
}
}
}
看14
、22
行代码,正如上面我所说,会将Queue
中的request
全部加入到mCacheQueue
中。
好了RequestQueue
的主要源码差不多就这些,下面我们进入CacheDispatcher
的源码分析,看它究竟如何工作的呢?
CacheDispatcher
前面提到了它与NetworkDispatcher
本质都是Thread
,那么我们自然是看run
方法
run
@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
对应不同的解析方法。例如StringRequest
与JsonObjectRequest
有各自的解析实现。再看45
行,发现不管entry
是否需要更新的,都会进一步对response
进行mDelivery.postResponse(request, response)
递送,不同的是需要更新的话重新设置request
的entry
与加入到mNetworkQueue
中,也就相当与重新进行网络请求一遍。那么再回到递送的阶段,前面已经提到在创建RequestQueue
是实现了ExecutorDelivery
,mDelivery.postResponse
就是其中的方法。我们来看一下
ExecutorDelivery
在这里创建了一个Executor
,对后面进行递送,作用在主线程
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
@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
进行执行,在进入ResponseDeliveryRunnable
的run
看它如何执行
ResponseDeliveryRunnable
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
行代码,对于不同的响应做不同的递送,deliverResponse
与deliverError
内部分别调用的就是我们非常熟悉的Listener
中的onResponse
与onErrorResponse
方法,进而返回到我们对网络请求结果的处理函数。
这就是整个的缓存派遣,简而言之,存在请求响应的缓存数据就不进行网络请求,直接调用缓存中的数据进行分发递送。反之执行网络请求。
下面我来看下NetworkDispatcher
是如何处理的
NetworkDispatcher
run
@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
的封装,分别调用HurlStack
与HttpClientStack
各自的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
递送。
如果有所帮助欢迎关注我的下一次解析
更多分享:个人博客