整体的执行流程
执行流程这一块我们基本上可以分为两大块
1. Volley.newRequestQueue(Context); 2. RequestQueue.add(Request); 3. 请求加入列表后的操作
1. Volley.newRequestQueue(Context)
模块内部执行流程
代码
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, 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) {//注释 1
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);//注释 2
RequestQueue queue;//注释 3
if (maxDiskCacheBytes <= -1)
{
// No maximum size specified
queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
}
else
{
// Disk cache size specified
queue = new RequestQueue(new DiskBasedCache(cacheDir, maxDiskCacheBytes), network);
}
queue.start();//注释 4
return queue;
}
上述代码理解
注释 1
,这里的作用是判断android版本,如果大于等于2.3的话就使用基于HttpURLConnection实现的HurlStack对象,否则就使用基于HttpClient实现的HttpClientStack对象。
注释 2
,BasicNetwork对象是使用stack网络处理数据,然后对数据进行简单处理,尤其是对异常和非异常的区分处理,主要是做异常的抛出与否
注释 3
,此处是创建RequestQueue对象,里面还创建了两个PriorityBlockingQueue作用已在while(true)死循环的执行和结束跳出进行大约阐明。这里面还创建了一个分发对象ExecutorDelivery
:此对象的作用是将网络访问结果分发给主线程内去执行,所以其构造函数中包含有主线程Handler。
- 创建RequestQueue对象主要代码如下
private final PriorityBlockingQueue> mCacheQueue =
new PriorityBlockingQueue>();
private final PriorityBlockingQueue> mNetworkQueue =
new PriorityBlockingQueue>();
private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4;
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
- 根据上面的代码可以看出
mCacheQueue
和mNetworkQueue
都是声明时就已将创建和初始化完成,请求结果的分发对象ExecutorDelivery
是在构造时创建的。
注释 4
,此处是通过创建RequestQueue的对象,对其调用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();
}
}
- 通过上述代码我们可以看到RequestQueue对象创建了一个CacheDispatcher对象,即缓存调度线程;和几个NetworkDispatcher对象,即网络调度线程。默认mDispatchers数组个数是
DEFAULT_NETWORK_THREAD_POOL_SIZE = 4
,所以就有5个线程在后台运行并等待请求的到来。 - 其中NetworkDispatcher和CacheDispatcher是两个线程对象,在此我们先不做深入讨论,稍后会深入,但是此处要知道这两个对象持有着
mCacheQueue
和mNetworkQueue
两个阻塞Queue。
2. RequestQueue.add(Request)
模块内部执行流程
代码
public Request add(Request 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 (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}
// 如果此前有相同的请求切还没有返回结果的,就将其加入等待队列
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
Queue> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new LinkedList>();
}
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
} else {
// 没有的话就将其加入缓存队列
mWaitingRequests.put(cacheKey, null);
mCacheQueue.add(request);
}
return request;
}
}
- 上述代码的大体流程作用已在代码中说明,此处就不在赘述。这里根据上述可能会有点疑惑,如果能缓存且此前没有相同的请求不就直接把请求放入缓存队列里了吗,这个时候是怎么做网络请求的。此处先给出
答案
:缓存队列里找不到对应的缓存数据后会将请求重新加入到网络请求队列。 - 通过上面的代码我们可以看出,add方法并没有做出网络请求或者从缓存中读取数据的操作,而是将请求放入网络请求队列或者缓存队列。
上面将网络请求
request
加入到网络请求队列mNetworkQueue
或者缓存队mCacheQueue
列后,分别持有mNetworkQueue
和mCacheQueue
的CacheDispatcher
(缓存调度线程)和NetworkDispatcher
(网络调度线程)是怎么遍历和等待以及怎么联系起来,我们在之前while(true)死循环的执行和结束跳出
已经讲述。
3. 请求加入列表后, CacheDispatcher和NetworkDispatcher的操作
CacheDispatcher缓存调度线程的run()
方法代码如下所示
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();
Request> request;
while (true) {
// release previous request object to avoid leaking request object when mQueue is drained.
request = null;
try {
// 获取缓存列表中的一个请求
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 (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
}
//尝试获取缓存
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 (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
}
// 缓存数据有效,并对数据进行解析,并把数据回调给主线程
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() {
@Override
public 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());
}
}
}
- 上面的代码执行流程大体已经用注释标明了。开始就是从缓存队列中取出一个请求,判断是否已经取消请求了,如果没有取消请求则去取缓存。如果没有缓存,则将请求加入网络请求队列;如果有缓存则判断缓存是否已经过期,如果已经过期则加入网络请求队列;如果没有过期则使用
request.parseNetworkResponse()
解析数据,并通过mDelivery.postResponse(request, response)
将结果回调给主线程。 - 上面的
request.parseNetworkResponse()
是每一种请求类型对接口Request的实现,里面会根据不同的场景将数据解析成不同的类型,例如:String
类型、Bitmap
类型 -
Delivery.postResponse()
的作用是简单的分析数据成功与否,然后再通过request.deliverResponse
或者request.deliverError
将数据分发给SuccessListener
或者ErrorListener
,然后调用相对应的onResponse()
和onErrorResponse()
就把结果最终交付给我们实现想用的地方。
NetworkDispatcher缓存调度线程的run()
方法代码如下所示
@Override
public 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 {
// 从队列中取出请求.
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 (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
addTrafficStatsTag(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;
}
// 对数据进行解析.
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);
}
}
}
- 上面的代码主要是判断请求是否被取消,然后决定是否继续请求网络,并将数据返回给主线程。
- 着重强调网络请求的执行语句为
mNetwork.performRequest(request)
,他的实现类我我们之前传递进来的BasicNetWork类。 - 请求成功结果跟前面所述的有请求缓存的处理是一样的,通过
Delivery.postResponse()
将结果回调给主线程
我们下面就看一下 BasicNetwork.performRequest()方法
@Override
public NetworkResponse performRequest(Request> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map responseHeaders = Collections.emptyMap();
try {
// Gather headers.
Map headers = new HashMap();
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.5
entry.responseHeaders.putAll(responseHeaders);
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, entry.data,
entry.responseHeaders, true,
SystemClock.elapsedRealtime() - requestStart);
}
// Handle moved resources
if (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);
}
}
}
}
- 此处会使用
mHttpStack.performRequest(request, headers)
方法进行真正的网络请求,其中mHttpStack
对象就是最开始Volley.newRequestQueue()
调用时根据不同版本生成的HurlStack
或者HttpClientStack
。 - 还有此处会抛出很多网络请求异常交给
NetworkDispatcher
处理,然后NetworkDispatcher会交给ExecutorDelivery
回调给主线程
将结果回调给主线程的方法:Delivery.postResponse()
这个是上面所说的将结果回调给主线程的ExecutorDelivery
,在此只是将代码贴出,并将原来解释在下面再复制粘贴一遍。
@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));
}
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")
@Override
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();
}
}
}
- Delivery.postResponse()的作用是简单的分析数据成功与否,然后再通过
request.deliverResponse
或者request.deliverError
将数据分发给SuccessListener
或者ErrorListener
,然后调用相对应的onResponse()
和onErrorResponse()
就把结果最终交付给我们实现想用的地方
StringRequest的部分代码
public class StringRequest extends Request {
private Listener mListener;
public StringRequest(int method, String url, Listener listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
public StringRequest(String url, Listener listener, ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
if (mListener != null) {
mListener.onResponse(response);
}
}
- 这里主要是看
deliverResponse()
方法,此处将结果给到mListener
,让其调用onResponse()
方法,返回给我们实例化StringRequest
对象的地方。
实例化StringRequest的地方
StringRequest mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
textView.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
textView.setText(volleyError.getMessage());
}
});
queue.add(mStringRequest);
- 此处我们实现了
Response.Listener
和Response.ErrorListener()
接口,最终结果将会回调到这里。
至此,Volley框架源码解析结束。
Volley--网络通讯框架 目录