RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); //第一步
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}); //第二步
queue.add(request); //第三步
按照Volley的使用方法,我们最先开始要初始化一个RequestQueue,由于新建一个RequestQueue非常消耗资源,开发的时候只需创建一次即可
newRequestQueue分析
public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
BasicNetwork network;
//自定义的网络栈
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
//如果API>=2.3 使用HttpUrlConnection的HurlStack
network = new BasicNetwork(new HurlStack());
} else {
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info =
context.getPackageManager().getPackageInfo(packageName, /* flags= */ 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
//API小于2.3时候使用httpclient
network =new BasicNetwork(new HttpClientStack(AndroidHttpClient.newInstance(userAgent)));
}
} else {
network = new BasicNetwork(stack);
}
//返回一个RequestQueue
return 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;
}
public void start() {
stop(); //停止所有的线程
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
//开启缓存线程
mCacheDispatcher.start();
//创建网络缓存池最大量的线程,并且启动线程 mDispatchers.length=4
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher =
new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
//停止网络和缓存的调度器
public void stop() {
if (mCacheDispatcher != null) {
mCacheDispatcher.quit();
}
for (final NetworkDispatcher mDispatcher : mDispatchers) {
if (mDispatcher != null) {
mDispatcher.quit();
}
}
}
到这里我们发现Volley创建一个RequestQueue会在后台默认创建5个线程其中1个缓存线程和4个网络调度线程
//提供对请求队列执行缓存分类的线程
public class CacheDispatcher extends Thread {
//缓存请求队列
private final BlockingQueue<Request<?>> mCacheQueue;
//网络请求队列
private final BlockingQueue<Request<?>> mNetworkQueue;
//读取的缓存
private final Cache mCache;
//用于发布响应
private final ResponseDelivery mDelivery;
//线程存活标志
private volatile boolean mQuit = false;
// 管理具有相同缓存键的等待请求和重复数据消除请求的列表
private final WaitingRequestManager mWaitingRequestManager;
...
//既然是一个线程,我们必须从run开始分析
@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
//提高线程的优先级
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// 阻塞线程,用来初始化
mCache.initialize();
while (true) {
try {
//对缓存队列进行处理
processRequest();
} catch (InterruptedException e) {
// 处理缓存队列时,出现异常,关闭线程
if (mQuit) {
Thread.currentThread().interrupt();
return;
}
VolleyLog.e(
"Ignoring spurious interrupt of CacheDispatcher thread; "
+ "use quit() to terminate it");
}
}
}
private void processRequest() throws InterruptedException {
//从缓存分类队列获取请求,阻止直到//至少有一个可用
final Request<?> request = mCacheQueue.take();
//缓存线程的执行过程//******//
processRequest(request);
}
void processRequest(final Request<?> request) throws InterruptedException {
//给请求添加标记 主要是在日志中的输入.
request.addMarker("cache-queue-take");
//如果请求已经关闭,则添加完成标记 不让发送请求体
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
return;
}
// 得到缓存池中的缓存
Cache.Entry entry = mCache.get(request.getCacheKey());
//如果没有缓存
if (entry == null) {
request.addMarker("cache-miss");
//如果没有缓存,把当前的请求发送给网络调度线程,
if (!mWaitingRequestManager.
//maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
return;
}
//判断缓存是否过期
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
//缓存过期后,把过期的缓存添加到请求体中
request.setCacheEntry(entry);
//把请求体发送给网络调度线程去处理
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
mNetworkQueue.put(request);
}
return;
}
// 得到一个缓存.
request.addMarker("cache-hit");
//分析其数据以将其传递回请求
Response<?> response =
request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
if (!entry.refreshNeeded()) {
//未过期。只需给出响应
//响应添加到缓存
mDelivery.postResponse(request, response);
} else {
//如果软缓存。我们可以传递缓存的响应,但是我们还需要将请求发送到网络进行刷新
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
//响应体设置为刷新状态
response.intermediate = true;
//如果当前请求没有发送给等待列表
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
//将响应体发给用户,并且将请求体转发给网络调度器重新请求数据
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 {
//将响应的结果存入缓存
//后面会介绍这个方法
mDelivery.postResponse(request, response);
}
}
}
//等待请求管理器
private static class WaitingRequestManager implements Request.NetworkRequestCompleteListener {
//有重复请求的请求的临时区域
private final Map<String, List<Request<?>>> mWaitingRequests = new HashMap<>();
private final CacheDispatcher mCacheDispatcher;
WaitingRequestManager(CacheDispatcher cacheDispatcher) {
mCacheDispatcher = cacheDispatcher;
}
//请求接收到一个有效响应,可供其他等待请求使用
@Override
public void onResponseReceived(Request<?> request, Response<?> response) {
//缓存为null或者缓存过期
if (response.cacheEntry == null || response.cacheEntry.isExpired()) {
//未收到可用响应时
onNoUsableResponseReceived(request);
return;
}
//拿到请求体的缓存key
String cacheKey = request.getCacheKey();
List<Request<?>> waitingRequests;
synchronized (this) {
//处理当前请求体的缓存
waitingRequests = mWaitingRequests.remove(cacheKey);
}
if (waitingRequests != null) {
if (VolleyLog.DEBUG) {
VolleyLog.v(
"Releasing %d waiting requests for cacheKey=%s.",
waitingRequests.size(), cacheKey);
}
for (Request<?> waiting : waitingRequests) {
//处理所有排队的请求
mCacheDispatcher.mDelivery.postResponse(waiting, response);
}
}
}
//接收到一个有效请求响应,其他等待请求可以使用该响应
@Override
public synchronized void onNoUsableResponseReceived(Request<?> request) {
String cacheKey = request.getCacheKey();
//移除当前请求的缓存
List<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
if (waitingRequests != null && !waitingRequests.isEmpty()) {
if (VolleyLog.DEBUG) {
VolleyLog.v(
"%d waiting requests for cacheKey=%s; resend to network",
waitingRequests.size(), cacheKey);
}
//拿到当前队列的第一条请求
Request<?> nextInLine = waitingRequests.remove(0);
mWaitingRequests.put(cacheKey, waitingRequests);
nextInLine.setNetworkRequestCompleteListener(this);
try {
mCacheDispatcher.mNetworkQueue.put(nextInLine);
} catch (InterruptedException iex) {
VolleyLog.e("Couldn't add request to queue. %s", iex.toString());
Thread.currentThread().interrupt();
mCacheDispatcher.quit();
}
}
}
//对于可缓存请求,如果相同缓存键的请求已经在运行中,将其添加到队列中,等待该运行中请求完成
private synchronized boolean maybeAddToWaitingRequests(Request<?> request) {
String cacheKey = request.getCacheKey();
//判断请求队列中是否存在相同的缓存
if (mWaitingRequests.containsKey(cacheKey)) {
List<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new ArrayList<>();
}
request.addMarker("waiting-for-response");
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.d("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
return true;
} else {
//添加占位的缓存
mWaitingRequests.put(cacheKey, null);
request.setNetworkRequestCompleteListener(this);
if (VolleyLog.DEBUG) {
VolleyLog.d("new request, sending to network %s", cacheKey);
}
return false;
}
}
}
}
public class NetworkDispatcher extends Thread {
//缓存请求队列
private final BlockingQueue<Request<?>> mCacheQueue;
//网络请求队列
private final BlockingQueue<Request<?>> mNetworkQueue;
//读取的缓存
private final Cache mCache;
//用于发布响应
private final ResponseDelivery mDelivery;
//线程存活标志
private volatile boolean mQuit = false;
....
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) {
try {
//处理缓存队列
processRequest();
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
Thread.currentThread().interrupt();
return;
}
VolleyLog.e(
"Ignoring spurious interrupt of NetworkDispatcher thread; "
+ "use quit() to terminate it");
}
}
}
private void processRequest() throws InterruptedException {
Request<?> request = mQueue.take();
processRequest(request);
}
@VisibleForTesting
void processRequest(Request<?> request) {
//得到当前的时间
long startTimeMs = SystemClock.elapsedRealtime();
try {
request.addMarker("network-queue-take");
//请求已经取消
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
//通知网络监听器请求不可用
request.notifyListenerResponseNotUsable();
return;
}
//添加流量统计标记
addTrafficStatsTag(request);
//执行网络请求
NetworkResponse networkResponse = mNetwork.performRequest(request);
request.addMarker("network-http-complete");
// 如果服务器返回304,并且我们已经传递了一个响应,
//我们完成了不要传递第二个相同的响应
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
request.notifyListenerResponseNotUsable();
return;
}
//分析工作线程上的响应
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// 写入缓存
// 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");
}
//将响应体返回
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();
}
}
}
public class BasicNetwork implements Network {
...
@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();
// 进行缓存验证
if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(
HttpURLConnection.HTTP_NOT_MODIFIED,
/* data= */ null,
/* notModified= */ true,
SystemClock.elapsedRealtime() - requestStart,
responseHeaders);
}
// 合并缓存头和响应头 ,完成一次网络请求响应
List<Header> combinedHeaders = combineHeaders(responseHeaders, entry);
return new NetworkResponse(
HttpURLConnection.HTTP_NOT_MODIFIED,
entry.data,
/* notModified= */ true,
SystemClock.elapsedRealtime() - requestStart,
combinedHeaders);
}
//检查响应体是否完整(没有内容等..)
InputStream inputStream = httpResponse.getContent();
if (inputStream != null) {
responseContents =
inputStreamToBytes(inputStream, httpResponse.getContentLength());
} else {
//无内容的请求
responseContents = new byte[0];
}
// 记录请求很慢的响应
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
logSlowRequests(requestLifetime, request, responseContents, statusCode);
//错误处理
if (statusCode < 200 || statusCode > 299) {
throw new IOException();
}
//返回一个完整的网络响应
return new NetworkResponse(
statusCode,
responseContents,
/* notModified= */ false,
SystemClock.elapsedRealtime() - requestStart,
responseHeaders);
}
...//异常处理
}
...
}
mDelivery.postResponse(request, response);
//实现类
public class ExecutorDelivery implements ResponseDelivery {
...
@Override
public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
request.markDelivered();
request.addMarker("post-response");
//主线程发布一个响应 mResponsePoster为主线程创建的线程池
mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
}
...
private class ResponseDeliveryRunnable implements Runnable {
@SuppressWarnings("unchecked")
@Override
public void run() {
// 请求关闭,
if (mRequest.isCanceled()) {
mRequest.finish("canceled-at-delivery");
return;
}
//请求结果是否成功
if (mResponse.isSuccess()) {
mRequest.deliverResponse(mResponse.result);
} else {
mRequest.deliverError(mResponse.error);
}
//中间的标记 ,
if (mResponse.intermediate) {
mRequest.addMarker("intermediate-response");
} else {
mRequest.finish("done");
}
if (mRunnable != null) {
mRunnable.run();
}
}
}
}
//成功
@Override
protected void deliverResponse(String response) {
Response.Listener<String> listener;
synchronized (mLock) {
listener = mListener;
}
if (listener != null) {
listener.onResponse(response);
}
}
//错误 这个回调在Request中就处理了
public void deliverError(VolleyError error) {
Response.ErrorListener listener;
synchronized (mLock) {
listener = mErrorListener;
}
if (listener != null) {
listener.onErrorResponse(error);
}
}
public <T> Request<T> add(Request<T> request) {
// 标记这个请求体
request.setRequestQueue(this);
synchronized (mCurrentRequests) {
//把请求体添加到请求集合中
mCurrentRequests.add(request);
}
// 按照添加顺序来执行网络请求
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue");
// 判断请求是否能缓存 不能缓存直接跳过缓存队列执行网络请求
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}
mCacheQueue.add(request);
return request;
}
//初始化RequestQueue中创建了一个Executor异步
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(
cache,
network,
threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
//当ExecutorDelivery.run执行完毕后回执行 handler.post(command);完成线程调度
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);
}
};
}
总共开启了5个线程:1个缓存调度线程和4个网络调度线程,并且线程的优先级为10,即后台线程。Volley其实并没有开启线程池去维护线程
public class HttpHeaderParser {
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
...
//Data 服务器返回的时间
headerValue = headers.get("Date");
if (headerValue != null) {
serverDate = parseDateAsEpoch(headerValue);
}
//缓存控制器
headerValue = headers.get("Cache-Control");
if (headerValue != null) {
hasCacheControl = true;
String[] tokens = headerValue.split(",");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
//不缓存响应数据,如果需要缓存响应数据,
if (token.equals("no-cache") || token.equals("no-store")) {
return null;
//当需要设置缓存时,通过maxAge的值来设置缓存过期的时间。
} else if (token.startsWith("max-age=")) {
try {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
} else if (token.startsWith("stale-while-revalidate=")) {
try {
staleWhileRevalidate = Long.parseLong(token.substring(23));
} catch (Exception e) {
}
//缓存数据过期前,可以使用缓存 如果过期必须强制对服务器进行校验
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
mustRevalidate = true;
}
}
}
//设置缓存过期的时间
headerValue = headers.get("Expires");
if (headerValue != null) {
serverExpires = parseDateAsEpoch(headerValue);
}
headerValue = headers.get("Last-Modified");
if (headerValue != null) {
lastModified = parseDateAsEpoch(headerValue);
}
serverEtag = headers.get("ETag");
}
}
用到的Http协议的字段如下: