Volley 是 Google 推出的輕量級 Android 異步網絡請求框架和圖片加載框架。在 Google I/O 2013 大會上發布。其適用場景是數據量小,通信頻繁的網絡操作。
(2). 一定程度符合 Http 規范,包括返回 ResponseCode(2xx、3xx、4xx、5xx)的處理,請求頭的處理,緩存機制的支持等。並支持重試及優先級定義。
(3). 默認 Android2.3 及以上基於 HttpURLConnection,2.3 以下基於 HttpClient 實現。
(4). 提供簡便的圖片加載工具。
創建RequestQueue很簡單,調用Volley類的靜態方法newRequestQueue,並指定Context即可:
private RequestQueue mQueue = null; // create request queue... mQueue = Volley.newRequestQueue(this);//this代表當前的上下文
String url = "http://192.168.56.1:8080";
StringRequest request = new StringRequest(url,new Response.Listener<String>()
{
@Override
public void onResponse(String response)//success callbacks
{
//handle it
}
}, new Response.ErrorListener()//error callbacks
{
@Override
public void onErrorResponse(VolleyError error)
{
error.printStackTrace();
}
});
//add request to queue...
mQueue.add(request);
Map<String,String> params = new HashMap<String,String>();
params.put("name","zhangsan");
params.put("age","17");
JSONObject jsonRequest = new JSONObject(params);
Log.i(TAG,jsonRequest.toString());
//如果json數據為空則是get請求,否則是post請求
//如果jsonrequest不為null,volley會將jsonObject對象轉化為json字符串原封不動的發給服務器,並不會轉成k-v對,因為volley不知道應該如何轉化
String url = "http://192.168.56.1:8080/volley_test/servlet/JsonServlet";
JsonObjectRequest request = new JsonObjectRequest(url, jsonRequest, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
//handle it
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
error.printStackTrace();
}
});
mQueue.add(request);
ImageRequest request = new ImageRequest("http://192.168.56.1:8080/volley_test/image.jpg",new Response.Listener<Bitmap>()
{
@Override
public void onResponse(Bitmap response)
{
mImageView.setImageBitmap(response);
}
},0,0, Bitmap.Config.ARGB_8888,new Response.ErrorListener()
{//參數0 0 代表不壓縮
@Override
public void onErrorResponse(VolleyError error)
{
show(error.getMessage());
//可以去顯示默認圖片
}
});
mQueue.add(request);
String url = "http://192.168.56.1:8080/volley_test/servlet/JsonServlet";
JsonObjectRequest request = new JsonObjectRequest(url, null,resplistener,errlistener)
{
//添加自定義請求頭
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String,String> map = new HashMap<String,String>();
map.put("header1","header1_val");
map.put("header2","header2_val");
return map;
}
};
String url = "http://192.168.56.1:8080/volley_test/servlet/PostServlet";
StringRequest request = new StringRequest(Method.POST,url,listener, errorListener)
{
//post請求需要復寫getParams方法
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
Map<String,String> map = new HashMap<String,String>();
map.put("KEY1","value1");
map.put("KEY2", "value2");
return map;
}
};
Request req = ...;
request.setTag("MAIN_ACTIVITY");
onDestroy()
{
...
mQueue.cancelAll("MAIN_ACTIVITY");
}
RequestQueue mQueue = ...;
ImageCache mCache = ...;
loader = new ImageLoader(mQueue,mImageCache);
ImageListener listener = ImageLoader.getImageListener(mImageView/*關聯的iamgeView*/,R.drawable.ic_launcher/*圖片加載時顯示*/, R.drawable.task_icon/*圖片加載失敗時顯示*/);
loader.get("http://192.168.56.1:8080/volley_test/image.jpg", listener, 0, 0);
public interface ImageCache {
public Bitmap getBitmap(String url);
public void putBitmap(String url, Bitmap bitmap);
}
/**
* @author Rowandjj
*圖片緩存需要做成單例,全局共享
*/
private static class LruImageCache implements ImageCache
{
private LruImageCache(){}
private static LruImageCache instance = new LruImageCache();
public static final LruImageCache getInstance()
{
return instance;
}
private static final String TAG = "LruImageCache";
private final int maxSize = (int) (Runtime.getRuntime().maxMemory()/8);
private LruCache<String,Bitmap> mCacheMap = new LruCache<String,Bitmap>(maxSize)
{
protected int sizeOf(String key, Bitmap value)
{
return value.getRowBytes()*value.getHeight();
}
};
@Override
public Bitmap getBitmap(String url)
{
Bitmap bitmap = mCacheMap.get(url);
Log.i(TAG, "url = "+url+",cache:"+bitmap);
return bitmap;
}
@Override
public void putBitmap(String url, Bitmap bitmap)
{
Log.i(TAG, "put url = "+url);
mCacheMap.put(url, bitmap);
}
}
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/niv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
ImageLoader loader = ...;
mNetImageView = findViewById(R.id.niv);
mNetImageView.setDefaultImageResId(R.drawable.ic_launcher);
mNetImageView.setErrorImageResId(R.drawable.task_icon);
mNetImageView.setImageUrl("http://192.168.56.1:8080/volley_test/image.jpg", loader);
public class XMLRequest extends Request<XmlPullParser>
{
private Listener<XmlPullParser> mListener;
public XMLRequest(int method, String url, Listener<XmlPullParser> listener,
ErrorListener errorListener)
{
super(method, url, errorListener);
mListener = listener;
}
public XMLRequest(String url, Listener<XmlPullParser> listener,
ErrorListener errorListener)
{
this(Method.GET, url, listener, errorListener);
}
@Override
protected Response<XmlPullParser> parseNetworkResponse(NetworkResponse response)
{
try
{
String xmlString = new String(response.data,HttpHeaderParser.parseCharset(response.headers));
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader(xmlString));//將返回數據設置給解析器
return Response.success(parser,HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e)
{
return Response.error(new VolleyError(e));
} catch (XmlPullParserException e)
{
return Response.error(new VolleyError(e));
}
}
@Override
protected void deliverResponse(XmlPullParser response)
{
mListener.onResponse(response);
}
}
使用方式:
/**
* xmlRequest 使用示例
*/
void test()
{
RequestQueue queue = Volley.newRequestQueue(context);
String url = "";
XMLRequest request = new XMLRequest(url,new Response.Listener<XmlPullParser>()
{
@Override
public void onResponse(XmlPullParser response)
{
int type = response.getEventType();
while(type != XmlPullParser.END_DOCUMENT)
{
switch (type)
{
case XmlPullParser.START_TAG:
break;
case XmlPullParser.END_TAG:
break;
default:
break;
}
response.next();
}
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
}
});
}
其實除了定制自己的Request,我們還可以定制好多東西,比如RequestQueue,參看RequestQueue的構造器:
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery)
//Volley.java
public static RequestQueue newRequestQueue(Context context) {
return newRequestQueue(context, null);
}
調用另一個工廠方法:
//volley.java
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
... ...
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;
}
//RequestQueue.java
public RequestQueue(Cache cache, Network network) {
this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
}
指定默認線程池大小為4。
//RequestQueue.java
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;
}
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();
}
}
邏輯很簡單,創建了CacheDispatcher和4個NetworkDispatcher個對象,然后分別啟動之。這個CacheDispatcher和NetworkDispatcher都是Thread的子類,其中CacheDispatcher處理走緩存的請求,而4個NetworkDispatcher處理走網絡的請求。CacheDispatcher通過構造器注入了緩存請求隊列(mCacheQueue),網絡請求隊列(mNetworkQueue),硬盤緩存對象(DiskBasedCache),結果分發器(mDelivery)。之所以也注入網絡請求隊列是因為一部分緩存請求可能已經過期了,這時候需要重新從網絡獲取。NetworkDispatcher除了緩存請求隊列沒有注入,其他跟CacheDispatcher一樣。到這里RequestQueue的任務就完成了,以后有請求都會交由這些dispatcher線程處理。

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 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;
}
}
通過這一方法,請求就被分發到兩個隊列中分別供CacheDispatcher和NetworkDispatcher處理。

CacheDispatcher.java#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 reques
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;
}
}
}
大體邏輯是這樣的,首先從隊列中取出請求,看其是否已被取消,若是則返回,否則繼續向下走。接着從硬盤緩存中通過緩存的鍵找到值(Cache.Entry),如果找不到,那么將此請求加入網絡請求隊列。否則對緩存結果進行過期判斷(這個需要請求的頁面指定了Cache-Control或者Last-Modified/Expires等字段,並且Cache-Control的優先級比Expires更高。否則請求一定是過期的),如果過期了,則加入網絡請求隊列。如果沒有過期,那么通過request.parseNetworkResponse方法將硬盤緩存中的數據封裝成Response對象(Request的parseNetworkResponse是抽象的,需要復寫)。最后進行新鮮度判斷,如果不需要刷新,那么調用ResponseDelivery結果分發器的postResponse分發結果。否則先將結果返回,再將請求交給網絡請求隊列進行刷新。【這段代碼讀起來很爽,google工程師寫的太贊了!】關於ResponseDelivery的具體過程我們留到下節講。

2.走網絡的請求
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Request request;
while (true) {
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;
}
// Tag the request (if API >= 14)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
TrafficStats.setThreadStatsTag(request.getTrafficStatsTag());
}
// 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) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
}
}
這里的邏輯跟CacheDispatcher類似,也是構造Response對象,然后交由ResponseDelivery處理,但是這里的Response對象是通過NetworkResponse轉化的,而這個NetworkResponse是從網絡獲取的,這里最核心的一行代碼就是
NetworkResponse networkResponse = mNetwork.performRequest(request);
這個mNetwork是BasicNetwork對象,我們看其performRequest的實現:
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = new HashMap<String, String>();
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) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
request.getCacheEntry().data, responseHeaders, true);
}
// 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);
} 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);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents,
responseHeaders, false);
if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth",
request, new AuthFailureError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}
}
}
這里最核心的是這一句:
httpResponse = mHttpStack.performRequest(request, headers);
它調用了HttpStack的performRequest,這個方法內部肯定會調用HttpURLConnection或者是HttpClient去請求網絡。這里我們就不必繼續向下跟源碼了。
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
ExecutorDelivery內部有個自定義Executor,它僅僅是封裝了Handler,所有待分發的結果最終會通過handler.post方法交給UI線程。
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, Runnable runnable) {
request.markDelivered();
request.addMarker("post-response");
mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
}
@Override
public void postResponse(Request<?> request, Response<?> response) {
postResponse(request, response, null);
}
最終執行的是ResponseDeliveryRunnable這個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();
}
}
}
這里我們看到了request.deliverResponse被調用了,這個方法通常會回調Listener.onResponse。哈哈,到這里,整個volley框架的主線就看完了!讀到這里,我真是由衷覺得google工程師牛逼啊!
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
continue;
}
如果請求取消就調用Request#finish,finish方法內部將調用與之綁定的請求隊列的finish方法,該方法內部會將請求對象在隊列中移除。
public void stop() {
if (mCacheDispatcher != null) {
mCacheDispatcher.quit();
}
for (int i = 0; i < mDispatchers.length; i++) {
if (mDispatchers[i] != null) {
mDispatchers[i].quit();
}
}
}
XXXDispatcher的quit方法會修改mQuit變量並調用interrupt使線程拋Interrupt異常,而Dispatcher捕獲到異常后會判斷mQuit變量最終while循環結束,線程退出。
catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}

public ImageContainer get(String requestUrl, ImageListener imageListener,
int maxWidth, int maxHeight) {
...
final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight);
// Try to look up the request in the cache of remote images.
Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
if (cachedBitmap != null) {
// Return the cached bitmap.
ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
imageListener.onResponse(container, true);
return container;
}
...
Request<?> newRequest =
new ImageRequest(requestUrl, new Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
onGetImageSuccess(cacheKey, response);
}
}, maxWidth, maxHeight,
Config.RGB_565, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onGetImageError(cacheKey, error);
}
});
mRequestQueue.add(newRequest);
mInFlightRequests.put(cacheKey,
new BatchedImageRequest(newRequest, imageContainer));
return imageContainer;
}
Servlet#doPost/doGet()
/*設置緩存*/
resp.setDateHeader("Last-Modified",System.currentTimeMillis());
resp.setDateHeader("Expires", System.currentTimeMillis()+10*1000*60);
resp.setHeader("Cache-Control","max-age=10000");
resp.setHeader("Pragma","Pragma");
Cache-Control字段的優先級高於Expires。這個可以從HttpHeaderParser#parseCacheHeaders方法中看到。
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
long now = System.currentTimeMillis();
Map<String, String> headers = response.headers;
long serverDate = 0;
long serverExpires = 0;
long softExpire = 0;
long maxAge = 0;
boolean hasCacheControl = false;
String serverEtag = null;
String headerValue;
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;
} else if (token.startsWith("max-age=")) {
try {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
maxAge = 0;
}
}
}
headerValue = headers.get("Expires");
if (headerValue != null) {
serverExpires = parseDateAsEpoch(headerValue);
}
serverEtag = headers.get("ETag");
// Cache-Control takes precedence over an Expires header, even if both exist and Expires
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
} else if (serverDate > 0 && serverExpires >= serverDate) {
// Default semantic for Expire header in HTTP specification is softExpire.
softExpire = now + (serverExpires - serverDate);
}
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = entry.softTtl;
entry.serverDate = serverDate;
entry.responseHeaders = headers;
return entry;
}
這個方法是由Request子類的parseNetworkResponse方法調用的:
Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response))

下面這幅圖也很好:

