前言
Volley我经常用也一直想阅读一下源码,今天抽出时间看了一下,将自己的理解记录一下,以便后续能快速的回忆起来。
先从用开始
看源码前我们还是先从简单的用开始吧,我们看源码也是围绕用法来看的。
String url = "https://www.baidu.com/";
StringRequest request = new StringRequest(Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request.setTag(url));
上面是volley最简单的用法,先实例化一个StringRequest ,然后再讲请求添加到请求队列中。
volley请求队列的实例化
Volley通过newRequestQueue将请求队列RequestQueue 实例化,newRequestQueue有三个重载方法,我们调用的一个参数的重载最终是指向2个参数的重载方法
public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
BasicNetwork network;
if (stack == null) {
//根据不同的系统版本号实例化不同的请求类,如果版本号小于9,用HttpClient
//如果版本号大于9,用HttpUrlConnection
if (Build.VERSION.SDK_INT >= 9) {
network = new BasicNetwork(new HurlStack());
} else {
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
network =
new BasicNetwork(
new HttpClientStack(AndroidHttpClient.newInstance(userAgent)));
}
} else {
network = new BasicNetwork(stack);
}
return newRequestQueue(context, network);
}
这个方法if else都在干的同一件事情就是实例化network ,唯一的区别是实例化network 的时候根据系统版本传进去的参数不同,如果版本大于等于9则使用HurlStack(里面封装了HttpURLConnection),小于9则使用HttpClientStack(里面封装了HttpClient)。
准备好参数network 后便调用真正实例化队列的方法newRequestQueue(context, network)。
private static RequestQueue newRequestQueue(Context context, Network network) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start();
return queue;
}
至此看到了调用构造方法实例化RequestQueue 。构造方法有两个参数,一个是network,一个是DiskBasedCache缓存。而后调用RequestQueue 的start方法。
再继续看一下RequestQueue 的构造方法
public RequestQueue(
Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) {
//初始化了RequestQueue的几个成员变量:mCache(文件缓存)、mNetwork(BasicNetwork实例)、mDispatchers(网络请求线程数组)、以及mDelivery(派发请求结果的接口)
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())));
}
public RequestQueue(Cache cache, Network network) {
this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
}
RequestQueue 的构造函数也是有好几个重载的,最终调用的上面这个方法,这个构造函数初始化了几个变量的值:mCache文件缓存、mNetwork是BasicNetwork实例、mDispatchers网络请求线程数组默认大小是4、mDelivery请求结果回调接口。
实例化好了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.
//首先实例化了CacheDispatcher,CacheDispatcher类继承自Thread,接着调用了它的start()方法,开始了一条新的缓存线程。
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
mCacheDispatcher.start();
// Create network dispatchers (and corresponding threads) up to the pool size.
//接着是一个for循环,根据设置的mDispatchers数组大小来开启多个网络请求线程,默认是4条网络请求线程。
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher =
new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
start方法实例化了一条缓存线程和默认4条网络请求线程,并开启start方法让线程循环等待任务执行。
网络请求的构造
请求队列RequestQueue 分析好了,那么就来看一下StringRequest 请求是怎么建立的。volley帮我们封装好了几种请求方式ImageRequest、JsonArrayRequest和JsonObjectRequest等。这几种方式的区别是针对不同的数据封装了不同的处理方式。理解了一种其他的都差不多,包括自定义请求。
public class StringRequest extends Request<String> {
/** Lock to guard mListener as it is cleared on cancel() and read on delivery. */
private final Object mLock = new Object();
// @GuardedBy("mLock")
private Listener<String> mListener;
public StringRequest(
int method, String url, Listener<String> listener, ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
public void cancel() {
super.cancel();
synchronized (mLock) {
mListener = null;
}
}
@Override
protected void deliverResponse(String response) {
Response.Listener<String> listener;
synchronized (mLock) {
listener = mListener;
}
if (listener != null) {
listener.onResponse(response);
}
}
@Override
@SuppressWarnings("DefaultCharset")
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
// Since minSdkVersion = 8, we can't call
// new String(response.data, Charset.defaultCharset())
// So suppress the warning instead.
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
代码量不多,StringRequest 继承Request<String>,deliverResponse和parseNetworkResponse方法是Request<String>的抽象方法,也就是任何请求都必须实现这两个方法。deliverResponse方法调用onResponse方法将请求结果回调到用户。parseNetworkResponse方法是对请求的数据做一些处理,StringRequest 也就是拼接成字符串返回,而如果是我们自定义的请求也是在这里做一些特定的数据处理,比如json的解析。
请求接入到队列中
请求也准备好了,最后一步是将请求通过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.
//标记当前请求,表示这个请求由当前RequestQueue处理
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;
}
mCacheQueue.add(request);
return request;
}
这里的关键代码是判断当前的网络请求是否允许缓存,如果允许缓存则加入到缓存队列,默认是允许缓存,否则加入到网络请求队列。
缓存线程CacheDispatcher
CacheDispatcher继承Thread,所以直接看线程的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 {
processRequest();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
}
}
}
线程一直在循环等待任务的执行
private void processRequest() throws InterruptedException {
// 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");
return;
}
// 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.
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
return;
}
// If it is completely expired, just send it to the network.
//判断缓存是否过期
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
return;
}
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
//先将响应的结果包装成NetworkResponse,然后调用Request子类的
//parseNetworkResponse方法解析数据
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.
//把处理好的数据post到主线程,这里用到了ExecutorDelivery#postResponse()方法
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;
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
// 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) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
}
});
} else {
// request has been added to list of waiting requests
// to receive the network response from the first request once it returns.
mDelivery.postResponse(request, response);
}
}
}
从缓存队列中取出请求,先判断请求是否关闭,而后再从缓存中取出请求结果,如果没有请求结果则将改请求加入到网络请求队列。如果缓存中有请求结果,则判断一下结果是否过期。得到请求结果后将调用StringRequest的parseNetworkResponse(我们这边是拿StringRequest做示例)处理结果,将结果封装成Response<?>,然后通过mDelivery.postResponse回调到主线程中
网络请求线程NetworkDispatcher
获取不到缓存将会通过网络请求获取结果,NetworkDispatcher也是继承自Thread,和缓存线程一样也是在run方法中循环等待执行processRequest。
private void processRequest() throws InterruptedException {
// Take a request from the queue.
Request<?> request = mQueue.take();
long startTimeMs = SystemClock.elapsedRealtime();
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");
request.notifyListenerResponseNotUsable();
return;
}
addTrafficStatsTag(request);
// Perform the network request.
///调用BasicNetwork实现类进行网络请求,并获得响应
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");
request.notifyListenerResponseNotUsable();
return;
}
// 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);
request.notifyListenerResponseReceived(response);
} catch (VolleyError volleyError) {
volleyError.setNetworkTimeMs(SystemClock.elapsedRealtime() - startTimeMs);
parseAndDeliverNetworkError(request, volleyError);
request.notifyListenerResponseNotUsable();
} 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);
request.notifyListenerResponseNotUsable();
}
}
processReques通过调用BasicNetwork实现类的方法performRequest进行网络请求,并获得响应,然后将结果缓存起来,最后回调到主线程。
performRequest实现如下
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
List<Header> responseHeaders = Collections.emptyList();
try {
// Gather headers.
Map<String, String> additionalRequestHeaders =
getCacheHeaders(request.getCacheEntry());
httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);
int statusCode = httpResponse.getStatusCode();
responseHeaders = httpResponse.getHeaders();
// Handle cache validation.
if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
...........................
}
// Some responses such as 204s do not have content. We must check.
InputStream inputStream = httpResponse.getContent();
if (inputStream != null) {
responseContents =
inputStreamToBytes(inputStream, httpResponse.getContentLength());
} 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, statusCode);
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
return new NetworkResponse(
statusCode,
responseContents,
false,
SystemClock.elapsedRealtime() - requestStart,
responseHeaders);
} catch (SocketTimeoutException e) {
............................
}
}
}
这里面主要的代码是调用了mBaseHttpStack.executeRequest,mBaseHttpStack这个之前实例化请求队列是根据版本实例化的HurlStack或HttpClientStack,executeRequest方法是对HttpURLConnection或HttpClient的封装。最后将网络请的结果封装成NetworkResponse回调到主线程。
delivery回调
最后一步,如果回调到主线程然后再回调到用户,volley是通过mDelivery.postResponse来实现的,mDelivery是在初始化请求队列的时候new出来的ExecutorDelivery实例
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);
}
};
}
@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));
}
构造函数中new了一个Executor并重写了execute方法,改方法中使用handler.post保证回调会在主线程中执行。postResponse中调用mResponsePoster.execute,其中一个参数是Runnable ,也就是ResponseDeliveryRunnable实现了runnable接口。
直接查看ResponseDeliveryRunnable实现的run方法。
@Override
public void run() {
// NOTE: If cancel() is called off the thread that we're currently running in (by
// default, the main thread), we cannot guarantee that deliverResponse()/deliverError()
// won't be called, since it may be canceled after we check isCanceled() but before we
// deliver the response. Apps concerned about this guarantee must either call cancel()
// from the same thread or implement their own guarantee about not invoking their
// listener after cancel() has been called.
// 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();
}
}
}
至此查看到回调到StringRequest的deliverResponse,最终回调到用户。