君不见,黄河之水天上来,奔流到海不复回。

君不见,高堂明镜悲白发,朝如青丝暮成雪!

人生得意须尽欢,莫使金樽空对月。

天生我材必有用,千金散尽还复来。

烹羊宰牛且为乐,会须一饮三百杯。

岑夫子,丹丘生,将进酒,杯莫停。

与君歌一曲,请君为我倾耳听。

钟鼓馔玉不足贵,但愿长醉不复醒。

古来圣贤皆寂寞,惟有饮者留其名。

陈王昔时宴平乐,斗酒十千恣欢谑。

主人何为言少钱,径须沽取对君酌。

五花马、千金裘,呼儿将出换美酒,与尔同销万古愁!

一首李白的《将进酒》送给大家。

本文已授权微信公众号《非著名程序员》原创首发,转载请务必注明出处。

闲聊Volley


Volley下载

Volley.jar及源码下载:http://download.csdn.net/detail/qq_17250009/9458711

Volley gitHub地址:https://github.com/mcxiaoke/android-volley

Volley特点

Google I/O 2013上发布了Volley!Volley是Android平台上的网络通信库,能使网络通信更快,更简单,更健壮!
名字由来:a burst or emission of many things or a large amount at once

1、特别适合数据量小,通信频繁的网络操作。
2、扩展性强。Volley 中大多是基于接口的设计,可配置性强。
3、一定程度符合 Http 规范,包括返回 ResponseCode(2xx、3xx、4xx、5xx)的处 理,请求头的处理,缓存机制的支持等。并支持重试及优先级定义。
4、默认 Android2.3 及以上基于 HttpURLConnection,2.3 以下基于 HttpClient 实现。
5、提供简便的图片加载工具。总之Volley就是很牛逼啦!

Volley执行流程图

英语好的看洋文,不行看国语。

Volley中的一些概念简介

Volley:Volley 对外暴露的 API,通过 newRequestQueue(…) 函数新建并启动一个请求队列RequestQueue。

Request:表示一个请求的抽象类。StringRequest、JsonRequest、ImageRequest 都是它的子类,表示某种类型的请求。

RequestQueue:表示请求队列,里面包含一个CacheDispatcher(用于处理走缓存请求的调度线程)、NetworkDispatcher数组(用于处理走网络请求的调度线程,默认长度为4),一个ResponseDelivery(返回结果分发接口),通过 start() 函数启动时会启动CacheDispatcher和NetworkDispatcher。

CacheDispatcher:一个线程,用于调度处理走缓存的请求。启动后会不断从缓存请求队列中取请求处理,队列为空则等待,请求处理结束则将结果传递给ResponseDelivery去执行后续处理。当结果未缓存过、缓存失效或缓存需要刷新的情况下,该请求都需要重新进入NetworkDispatcher去调度处理。

NetworkDispatcher:一个线程,用于调度处理走网络的请求。启动后会不断从网络请求队列中取请求处理,队列为空则等待,请求处理结束则将结果传递给ResponseDelivery去执行后续处理,并判断结果是否要进行缓存。

ResponseDelivery:返回结果分发接口,目前只有基于ExecutorDelivery的在入参 handler 对应线程内进行分发。

HttpStack:处理 Http 请求,返回请求结果。目前 Volley 中有基于 HttpURLConnection 的HurlStack和 基于 Apache HttpClient 的HttpClientStack。

Network:调用HttpStack处理请求,并将结果转换为可被ResponseDelivery处理的NetworkResponse。

Cache:缓存请求结果,Volley 默认使用的是基于 sdcard 的DiskBasedCache。NetworkDispatcher得到请求结果后判断是否需要存储在 Cache,CacheDispatcher会从 Cache 中取缓存结果。

Volley类关系图

其中红色框内组成了Volley的核心。


正式开始Volley之旅


Volley使用示例

    /*** 获取String类型的数据*/private void getStringData() {mQueue = Volley.newRequestQueue(this);// 接口来自聚合数据String url = "http://apis.juhe.cn/cook/queryid";StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {@Overridepublic void onResponse(String s) {Log.i(TAG, s);tv_content.setText(s);}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError volleyError) {Log.i(TAG, volleyError.getMessage());tv_content.setText(volleyError.getMessage());}} ){@Overrideprotected Map<String, String> getParams() throws AuthFailureError {Map<String, String> params = new HashMap<String , String>();params.put("key","a623b1b9a688bc174b2f92edc4f6008d");params.put("id","1001");return params;}};mQueue.add(stringRequest);}

可以看到,我们首先调用Volley.newRequestQueue(this)获取到一个RequestQueue。那么在这期间,源代码做了什么呢?

    public static RequestQueue newRequestQueue(Context context) {return newRequestQueue(context, null);}public static RequestQueue newRequestQueue(Context context, HttpStack stack){return newRequestQueue(context, stack, -1);}public static RequestQueue newRequestQueue(Context context, int maxDiskCacheBytes) {return newRequestQueue(context, null, maxDiskCacheBytes);}/** Default on-disk cache directory. */private static final String DEFAULT_CACHE_DIR = "volley";public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {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.htmlstack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));}}Network network = new BasicNetwork(stack);RequestQueue queue;if (maxDiskCacheBytes <= -1){// No maximum size specifiedqueue = new RequestQueue(new DiskBasedCache(cacheDir), network);}else{// Disk cache size specifiedqueue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network);}queue.start();return queue;}

本篇博客大部分在解释上面一段代码的执行流程及设计思想。首先创建data/data/packageName/Volley文件夹作为缓存目录。如果stack = null。如果API Level >= 9,采用基于 HttpURLConnection 的 HurlStack,如果小于 9,采用基于 HttpClient 的 HttpClientStack。接着构建了一个基于BasicNetWork的network,然后new DiskBaseCache(cacheDir),紧接着用new DiskBaseCache(cacheDir)和network作为参数构建一个RequestQueue

    /** Number of network request dispatcher threads to start. */private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4;public RequestQueue(Cache cache, Network network) {this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);}public RequestQueue(Cache cache, Network network, int threadPoolSize) {this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper())));}public RequestQueue(Cache cache, Network network, int threadPoolSize,ResponseDelivery delivery) {mCache = cache;mNetwork = network;mDispatchers = new NetworkDispatcher[threadPoolSize];mDelivery = delivery;}

在这里初始化了一些相关参数,下面是参数解释说明。
mCache : 基于DiskBasedCache的Cache对象
mNetwork : 基于BasicNetwork的Network对象
mDispatchers : 网络请求线程数组,默认大小为4
mDelivery : 基于ExecutorDelivery的ResponseDelivery对象

最后调用queue.start()就可以使用了。至此,使用Volley相关的参数已经初始化完毕,有没有觉得使用很方便呢?知其然知其所以然,跟进代码RequestQueue#start()

    /*** 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();}}

首先stop之前的调用,遍历quit掉五个线程(接下来会详细介绍着五个线程),这个代码很简单并且现在不好理解,直接略过了。回头你会明白的。接下来新建并start一个CacheDispatcher。新建并start四个(mDispatchers 默认大小为4)NetworkDiapatcher,并组成长度为4的数组。1+4就是上文中提到的5个线程。为了方便学习,我们首先查看NetworkDiapatcher类。

NetworkDiapatcher

public class NetworkDispatcher extends Thread{...public NetworkDispatcher(BlockingQueue<Request<?>> queue,Network network, Cache cache,ResponseDelivery delivery) {mQueue = queue;mNetwork = network;mCache = cache;mDelivery = delivery;}@Overridepublic void run() {Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);Request<?> request;while (true) {long startTimeMs = SystemClock.elapsedRealtime();// release previous request object to avoid leaking request object when mQueue is drained.request = null;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);}}}...
}

代码有些长,只要我们细心去查看,还是能发现写蛛丝马迹的。NetworkDispatcher是一个线程,在run方法中是个while(true)死循环,不断从mQueue(RequestQueue.mNetworkQueue)中取出request。然后调用mNetwork.performRequest(request),实际调用的是Network的具体实现BasicNetWork.performRequest(request)。下面我们看一下BasicNetWork.performRequest(request)

BasicNetwork

public class BasicNetwork implements Network {...@Overridepublic NetworkResponse performRequest(Request<?> request) throws VolleyError {long requestStart = SystemClock.elapsedRealtime();while (true) {HttpResponse httpResponse = null;byte[] responseContents = null;Map<String, String> responseHeaders = Collections.emptyMap();try {// Gather headers.Map<String, String> headers = new HashMap<String, String>();addCacheHeaders(headers, request.getCacheEntry());httpResponse = mHttpStack.performRequest(request, headers);StatusLine statusLine = httpResponse.getStatusLine();int statusCode = statusLine.getStatusCode();responseHeaders = convertHeaders(httpResponse.getAllHeaders());// Handle cache validation.if (statusCode == HttpStatus.SC_NOT_MODIFIED) {Entry entry = request.getCacheEntry();if (entry == null) {return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, null,responseHeaders, true,SystemClock.elapsedRealtime() - requestStart);}// A HTTP 304 response does not have all header fields. We// have to use the header fields from the cache entry plus// the new ones from the response.// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5entry.responseHeaders.putAll(responseHeaders);return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data,entry.responseHeaders, true,SystemClock.elapsedRealtime() - requestStart);}// Handle moved resourcesif (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {String newUrl = responseHeaders.get("Location");request.setRedirectUrl(newUrl);}// Some responses such as 204s do not have content.  We must check.if (httpResponse.getEntity() != null) {responseContents = entityToBytes(httpResponse.getEntity());} else {// Add 0 byte response as a way of honestly representing a// no-content request.responseContents = new byte[0];}// if the request is slow, log it.long requestLifetime = SystemClock.elapsedRealtime() - requestStart;logSlowRequests(requestLifetime, request, responseContents, statusLine);if (statusCode < 200 || statusCode > 299) {throw new IOException();}return new NetworkResponse(statusCode, responseContents, responseHeaders, false,SystemClock.elapsedRealtime() - requestStart);} catch (SocketTimeoutException e) {attemptRetryOnException("socket", request, new TimeoutError());} catch (ConnectTimeoutException e) {attemptRetryOnException("connection", request, new TimeoutError());} catch (MalformedURLException e) {throw new RuntimeException("Bad URL " + request.getUrl(), e);} catch (IOException e) {int statusCode = 0;NetworkResponse networkResponse = null;if (httpResponse != null) {statusCode = httpResponse.getStatusLine().getStatusCode();} else {throw new NoConnectionError(e);}if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {VolleyLog.e("Request at %s has been redirected to %s", request.getOriginUrl(), request.getUrl());} else {VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());}if (responseContents != null) {networkResponse = new NetworkResponse(statusCode, responseContents,responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);if (statusCode == HttpStatus.SC_UNAUTHORIZED ||statusCode == HttpStatus.SC_FORBIDDEN) {attemptRetryOnException("auth",request, new AuthFailureError(networkResponse));} else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {attemptRetryOnException("redirect",request, new RedirectError(networkResponse));} else {// TODO: Only throw ServerError for 5xx status codes.throw new ServerError(networkResponse);}} else {throw new NetworkError(e);}}}}...
}

代码依旧很长,不过不要灰心,我们只看关键的代码就可以了。在BssicNetWork.performRequest()中由HttpStack(具体实现是HurlStack或者HttpClientStack).performRequest(request)获取数据,最后BasicNetWork.performRequest()跟据各种情况返回不同的NetworkResponse对象。

返回NetworkDispatcher#run()接着看

获取到NetworkResponse后,执行request.parseNetWorkReqponse,并返回一个Response<?>对象。这也是为什么自定义的request必须重写parseNetWorkReqponse()方法的原因。最后执行ResponseDelivery(具体实现是ExecutorDelivery).postResponse(request,response)进行分发。

四个一样的NetWorkDispatcher不断的在后台运行,至此,NetWorkDispatcher分析完毕。接下来我们来看CatchDispatcher类(一定要看懂NetworkDispatcher类再看这个CatchDispatcher类,因为会涉及到。这也是为什么先讲NetworkDispatcher的原因)。

CatchDispatcher

public class CacheDispatcher extends Thread {private static final boolean DEBUG = VolleyLog.DEBUG;/** The queue of requests coming in for triage. */private final BlockingQueue<Request<?>> mCacheQueue;/** The queue of requests going out to the network. */private final BlockingQueue<Request<?>> mNetworkQueue;/** The cache to read from. */private final Cache mCache;/** For posting responses. */private final ResponseDelivery mDelivery;/** Used for telling us to die. */private volatile boolean mQuit = false;public CacheDispatcher(BlockingQueue<Request<?>> cacheQueue, BlockingQueue<Request<?>> networkQueue,Cache cache, ResponseDelivery delivery) {mCacheQueue = cacheQueue;mNetworkQueue = networkQueue;mCache = cache;mDelivery = delivery;}public void quit() {mQuit = true;interrupt();}@Overridepublic 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();Request<?> request;while (true) {// release previous request object to avoid leaking request object when mQueue is drained.request = null;try {// Take a request from the queue.request = mCacheQueue.take();} catch (InterruptedException e) {// We may have been interrupted because it was time to quit.if (mQuit) {return;}continue;}try {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.final Request<?> finalRequest = request;mDelivery.postResponse(request, response, new Runnable() {@Overridepublic void run() {try {mNetworkQueue.put(finalRequest);} catch (InterruptedException e) {// Not much we can do about this.}}});}} catch (Exception e) {VolleyLog.e(e, "Unhandled exception %s", e.toString());}}}
}

又是这么长,阿西吧!不过没关系,分析完NetWorkDispatcher再来看这个CatchDispatcher就很简单了,因为这两个类有很多相同的地方。

CatchDispatcher也是一个线程。在run()方法中也是一个while(true)死循环。不断从mCacheQueue中取出一个request。然后尝试从mCache中去查找request中key(url)对应的entry。如果entry = null或者过期,则直接插入mNetWorkQueue。在分析NetworkDispatcher的时候提到在run方法中会不断从mQueue(RequestQueue.mNetWorkQueue)中取出request,然后获取数据,解析数据,封装数据,分发。此处流程和Loop不断从MessageQueue中取出Message过程很像,但这里是Loop和MessageQueue交互的升级版,类似于一重循环和二重循环。对Loop、MessageQueue一级循环感兴趣的小伙伴可以看下我的另一篇博客:Handler、Message、MessageQueue、Looper调用过程源码浅析,在此不在赘述。如果entry不为空也没有过期,则认为可以从mCache中直接获取。后续过程和NetWorkDispatcher类似,封装数据,分发。和NetWorkDispatcher不同的一点是还要检查是否需要刷新数据,不需要刷新数据,过程和NetWorkDispatcher一样。需要刷新,则插入mNetWorkQueue。

在获取RequstQueue实例的时候new ExecutorDelivery(new Hanlder(Looper.getMainLooper()))。这里的作用就是切换线程,而且是切换到主线程。两个参数的ExecutorDelivery.postResponse()方法调用三个参数的ExecutorDelivery.postResponse()方法时new 了一个实现了Runnable接口的ResponseDeliveryRunnable对象。看一下ExecutorDelivery类。

ExecutorDelivery

public class ExecutorDelivery implements ResponseDelivery {/** Used for posting responses, typically to the main thread. */private final Executor mResponsePoster;/*** Creates a new response delivery interface.* @param handler {@link Handler} to post responses on*/public ExecutorDelivery(final Handler handler) {// Make an Executor that just wraps the handler.mResponsePoster = new Executor() {@Overridepublic void execute(Runnable command) {handler.post(command);}};}/*** Creates a new response delivery interface, mockable version* for testing.* @param executor For running delivery tasks*/public ExecutorDelivery(Executor executor) {mResponsePoster = executor;}@Overridepublic void postResponse(Request<?> request, Response<?> response) {postResponse(request, response, null);}@Overridepublic void postResponse(Request<?> request, Response<?> response, Runnable runnable) {request.markDelivered();request.addMarker("post-response");mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));}@Overridepublic void postError(Request<?> request, VolleyError error) {request.addMarker("post-error");Response<?> response = Response.error(error);mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, null));}/*** A Runnable used for delivering network responses to a listener on the* main thread.*/@SuppressWarnings("rawtypes")private class ResponseDeliveryRunnable implements Runnable {private final Request mRequest;private final Response mResponse;private final Runnable mRunnable;public ResponseDeliveryRunnable(Request request, Response response, Runnable runnable) {mRequest = request;mResponse = response;mRunnable = runnable;}@SuppressWarnings("unchecked")@Overridepublic void run() {// If this request has canceled, finish it and don't deliver.if (mRequest.isCanceled()) {mRequest.finish("canceled-at-delivery");return;}// 分发核心代码块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();}}}
}

实例化ExecutorDelivery的时候传入了主线程的looper,这也就意味着分发直接post到主线程,可以直接更新UI。ResponseDeliveryRunnable#run()方法中分发mResponse。这也是为什么自定义的request必须重写deliverResponse()方法的原因,deliverError()方法在Request类中已经被重写过,所以不必重写。

至此,RequestQueue#start()及相关的NetWorkDispatcher(网络请求)、CatchDispatcher(缓存)、ExecutorDelivery(分发)类也已经看完,只剩最后一步RequestQueue.add(reuqest)了。同志们,胜利的曙光就在眼前!Fighting!

    /*** Adds a Request to the dispatch queue.* @param request The request to service* @return The passed-in request*/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插入到mCurrentRequests。如果request不应该被缓存(默认可以缓存,可以调用Request的setShouldCache(false)方法来改变这一默认行为),则直接插入mNetworkQueue。否则判断mWaitingRequests包不包含cacheKey(url)。

如果包含,则将此请求加入mWaitingRequests队列,不再重复请求,在上一个请求返回时直接发送结果,详见RequestQueu#finish()。

如果mWaitingRequests不包含cacheKey(url),则插入mWaitingRequests,然后执行mCacheQueue.add(request)。

上文分析CacheDispatcher时说到,run()方法也是个死循环,不断从mCacheQueue中取出request,然后走相应的流程。具体可查看CatchDispatcher分析,不再赘述。

至此,Volley源码解析完毕。回头再看Volley执行流程图是不是思路清晰的多。小伙伴们,get到了吗?

Android Volley核心源码解析相关推荐

  1. 面试官系统精讲Java源码及大厂真题 - 09 TreeMap 和 LinkedHashMap 核心源码解析

    09 TreeMap 和 LinkedHashMap 核心源码解析 更新时间:2019-09-05 10:15:03 人的影响短暂而微弱,书的影响则广泛而深远. --普希金 引导语 在熟悉 HashM ...

  2. RocketMQ源码系列(一) NameServer 核心源码解析

    目录 一.NameServer 介绍 二.NameServer 功能列表 三.NameServer 架构分析 四.NameServer 工程目录解析 五.NameServer 启动流程分析 1)  创 ...

  3. Sentinel核心源码解析

    Sentinel核心源码解析 Sentinel是分布式系统的防御系统.以流量为切入点,通过动态设置的流量控制.服务熔断等手段达到保护系统的目的,通过服务降级增强服务被拒后用户的体验. 一.Sentin ...

  4. 新书上市 | Vue 3.0 核心源码解析,这本书给Vue学习提供新方法

    Vue.js 作为一款极简的 MVVM 框架,因其轻量.易上手,得到了众多开发者的喜爱. 自从 2014 年 Vue 诞生以来,这个框架设计的初衷,尤大说只是为了设计一个让自己用起来舒服的框架,随着受 ...

  5. Netty 核心源码解析

    Netty 第一讲:Netty 架构与原理 本文是<Netty 三讲>的第二讲:Netty 核心源码解析(部分),大纲如下: 前言 1. Netty 服务端启动过程源码剖析 1.1. 执行 ...

  6. Kafka核心源码解析 - KafkaController源码解析

    在进入源码解析之前,我先来介绍一些KafkaController在Kafka集群中的作用. (1)负责监听zookeeper上所有的元数据变更请求: (2)负责topic的partition迁移任务分 ...

  7. android什么意思!读完我这份《Android开发核心源码精编解析》面试至少多要3K!面试建议

    开头 经常会有人问:"现在学习Android开发还有前景?"我的看法是现在只是市场趋于平稳了,对开发人员的要求越来越好了,这并不代表没有前景了. 移动开发不等于App开发,所有新的 ...

  8. 程序员进阶!读完我这份《Android开发核心源码精编解析》面试至少多要3K!已拿到offer

    开头 作为一个40的人,能有面试机会是格外的珍惜,也分外的诚恳.没什么豪言壮语,雄心大志.没有狼性,社会把中年人打磨成了听话的舔狗. 感谢马爸爸旗下公司,给了我为数不多机会中一个,而且还是个相当好的位 ...

  9. Vue3核心源码解析第一课 看懂Vue3的优化

    开篇词 解析 Vue.j 源码,提升编码能力 我现任 Zoom 前端架构师,曾先后于百度.滴滴从事前端研发工作.我平时喜欢钻研新技术.新框架,关注前端自动化.工程化.前端架构.和很多常年打磨自身编程能 ...

最新文章

  1. nginx安装路径,查找配置文件以及如何配置
  2. 【图解】《“十四五”机器人产业发展规划》
  3. 通过FILETIME得到时间
  4. ITK:两个图像的平方差
  5. iOS 文件和数据管理 (可能会删除本地文件储存)
  6. 互联网日报 | 1月22日 星期五 | 春节返乡防疫政策发布;滴滴成立技术委员会;2021全国网上年货节正式启动...
  7. java数组末尾添加元素_JavaScript 数组 Array对象增加和删除 元素
  8. Python从2.6升级到2.7,使用pip安装module,报错:No Module named pip.log(转载)
  9. 配置管理——配置项标识
  10. linux查看cpu核数命令,Linux系統下如何查看CPU型號、核心數量、頻率和溫度?
  11. PowerDesigner如何生产数据字典
  12. 医疗器械信号输入输出部分,以及电气绝缘图,环境试验后的标准
  13. eregi php5.4,PHP 5.4/5.3弃用函数eregi() memory_limit绕过漏洞
  14. meta http-equiv=“X-UA-Compatible“ content=““ 的作用
  15. 虚拟主机管理器WHM的详细介绍
  16. 最新总结-php根据ip获取所在位置(定位)
  17. 如何修改PDF文件内容,PDF怎么编辑页眉页脚
  18. 物联网安全威胁与解决方案调研
  19. 【操作系统二】图解TCP/IP模型+实战
  20. 我的电力行业求职经历(供电局电力设计院面经

热门文章

  1. QT常用函数总结(全)
  2. 研究生哪些行为可以在导师那超加分?
  3. thinkphp5如何对接使用阿里云短信
  4. 【网络游戏植入案例】
  5. 编写程序实现乐手弹奏乐器。乐手可以弹奏不同的乐器从而发出不同的声音,可以弹奏的乐器包括二胡、钢琴和琵琶。
  6. 【C++面向对象程序设计——侯捷大师】心得摘要
  7. EBMIDE——延缓显示生成,优化用户响应
  8. WCF 之 什么是WCF
  9. HTTP协议与HTTPS协议详解(含常见面试题)
  10. 谷歌打开后开始页面被hao123篡改