主目錄見:Android高級進階知識(這是總目錄索引)
?這是我們第一篇講解網絡請求相關的框架,前面我們的所有講解都是跟網絡請求沒有關系,如果大家對Http協議的原理等還不熟悉,希望大家自己復習一下,畢竟還是要知其然知其所以然,首先為了大家對[Volley]有個整體的認識,我們先貼個圖:
這是官網的一個原理圖,大家看著這個應該能對整體的原理有個把握,思路還是非常清晰的,看圖可以看出藍色的是在主線程,綠色的是緩存調度線程,粉紅的是網絡調度線程,首先我們會調用RequestQueue的add方法把一個Request對象添加進緩存隊列中,如果在緩存隊列中發現有對應的請求結果,則直接解析然后傳遞給主線程,如果沒有的話那就放進網絡請求的隊列中,然后循環取出請求進行http請求,解析網絡返回的結果,寫入緩存中,最后把解析的結果傳遞給主線程 。
一.目標
?今天我們是講解網絡請求框架的第一篇,希望大家有個好的開始,接下來有時間會相應地講解OkHttp和retrofit的源碼,敬請期待,今天目標如下:
1.從源碼角度深刻了解Volley的源碼;
2.知道怎么自定義Request。
二.源碼解析
我們分析源碼從最基礎的用法入手,我們回顧下Volley的最基本用法,這里以StringRequest為例:
1.創建一個RequestQueue對象
RequestQueue mQueue = Volley.newRequestQueue(context);
2.創建一個StringRequest對象
StringRequest stringRequest = new StringRequest("http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
3.將StringRequest對象添加到RequestQueue里面。
mQueue.add(stringRequest);
這樣三步,最基本的使用就已經完成了,那么我們現在遵循這個步驟來進行源碼解析。
1.Volley newRequestQueue
首先我們看到Volley這個類里面的這個方法:
public static RequestQueue newRequestQueue(Context context) {
return newRequestQueue(context, (BaseHttpStack) null);
}
我們看到這個方法調用了兩個參數的同個方法,第二個參數傳的是null,我們就來看看兩個參數的這個方法:
public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
BasicNetwork network;
if (stack == null) {
//這個方法是public的,以為這我們可以自己傳一個stack進來,如果我們沒有傳進來則需要進行下面的操作
if (Build.VERSION.SDK_INT >= 9) {
network = new BasicNetwork(new HurlStack());
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
// At some point in the future we'll move our minSdkVersion past Froyo and can
// delete this fallback (along with all Apache HTTP code).
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 {
//如果有傳stack這個參數我們就使用他構造一個BasicNetwork對象
network = new BasicNetwork(stack);
}
return newRequestQueue(context, network);
}
我們看到我們這里會進行版本的判斷,如果android的版本大于等于9則使用HurlStack作為BasicNetwork的參數,如果小于9呢則讓HttpClientStack作為BasicNetwork作為參數,其中HurlStack用的是HttpURLConnection
訪問的網絡,HttpClientStack使用HttpClient
問網絡。最后又調用了另外一個newRequestQueue方法,我們一起來看看:
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;
}
我們看到這里得到一個File對象,這個File對象主要是指定緩存的目錄,然后傳給DiskBasedCache這個緩存類的(public class DiskBasedCache implements Cache),記住這個類,后面從緩存中取出數據和放入數據都是放的這里面。然后將這個緩存類和BasicNetwork傳給RequestQueue來構建這個對象。我們看下RequestQueue這個對象是怎么構建的:
public RequestQueue(Cache cache, Network network) {
this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
}
DEFAULT_NETWORK_THREAD_POOL_SIZE這里的值默認是4,我們看到這邊又調用了RequestQueue的三個參數的構造函數:
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
我們看到這里又出現了一個新的類ExecutorDelivery,這個類里面傳進了一個在主線中的Handler,其實這個類就是后面負責用來把解析完的網絡的結果返回給主線程的。同樣,這個類又調用了四個參數的構造函數:
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}
我們看到了這里面就是將緩存類DiskBasedCache,網絡訪問類BasicNetWork,還有利用threadPoolSize=4創建一個NetworkDispatcher數組,發送到主線程類ExecutorDelivery賦值給RequestQueue的變量中,后面都會用到,望大家注意!!!!
2.RequestQueue start
我們看到在獲取到RequestQueue對象完之后會調用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();
}
}
我們看到程序會new出一個CacheDispatcher對象,然后調用它的start()方法,CacheDispatcher
是一個Thread子類,就是上面圖片說的這是個緩存調度線程。然后我們看到會遍歷mDispatchers數組,這就是我們剛才用threadPoolSize=4創建的,我們這里遍歷創建四個NetworkDispatcher 對象,然后調用start()方法,同樣的NetworkDispatcher
同時也是一個Thread的子類,這就是前面圖片說的網絡調度線程。然后我們根據基本用法知道我們這時候還需要一個StringRequest請求對象。我們看下這個到底是什么。
3.StringRequest
這個類其實是繼承Request的類:
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;
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new GET request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
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
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
首先StringRequest是繼承自Request類的,Request可以指定一個泛型類,這里指定的當然就是String了,接下來StringRequest中提供了兩個有參的構造函數,參數包括請求類型,請求地址,以及響應回調等。但需要注意的是,在構造函數中一定要調用super()方法將這幾個參數傳給父類,因為HTTP的請求和響應都是在父類中自動處理的。
另外,由于Request類中的deliverResponse()和parseNetworkResponse()是兩個抽象方法,因此StringRequest中需要對這兩個方法進行實現。deliverResponse()方法中的實現很簡單,僅僅是調用了mListener中的onResponse()方法,并將response內容傳入即可,這樣就可以將服務器響應的數據進行回調了。parseNetworkResponse()方法中則應該對服務器響應的數據進行解析,其中數據是以字節的形式存放在NetworkResponse的data變量中的,這里將數據取出然后組裝成一個String,并傳入Response的success()方法中即可。
4.RequestQueue add
得到RequestQueue 的對象以及Request對象之后,我們需要將這個Request對象添加進這個Queue里面去。我們看看這個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.
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;
}
我們看到首先將request對象添加進mCurrentRequests里面,記錄當前的request請求。然后我們看到會判斷shouldCache()是否返回要緩存,一般情況下是true的,但是我們可以自己修改,調用setShouldCache()來改變這個值。如果要緩存則直接調用mCacheQueue#add()
方法將這個request添加進緩存隊列中。如果不需要緩存則直接添加進網絡請求隊列中去。
5.CacheDispatcher run
我們知道前面將request將請求添加進隊列里面,那么網絡請求線程和緩存請求線程的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是不是已經取消了,取消了就直接結束即可
request.finish("cache-discard-canceled");
continue;
}
// Attempt to retrieve this item from cache.
//從緩存中取出對應key的request是不是已經有緩存網絡返回結果了,如果有的話就直接返回結果
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);
}
continue;
}
// 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);
}
continue;
}
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
//調用request的parseNetworkResponse來解析網絡返回結果,因為網絡返回可能是xml,string,json等等數據,這個地方可以自定義一個request來解析
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;
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
//如果是需要刷新的則我們先返回結果,然后再把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 {
// 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);
}
}
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
}
}
}
我們看到上面的代碼就是說明的我們圖片里面的意思,程序會循環運行,首先會嘗試從緩存DiskBasedCache中取出請求的request,如果緩存DiskBasedCache中已經有這個緩存的結果,則先判斷返回結果有沒有過期或者需要重新刷新,如果都不需要則直接解析調用ExecutorDelivery返回給主線程即可。
6.NetworkDispatcher run
我們上面已經看了緩存的調度線程,我們現在來看網絡的調度線程里面做了些啥,我們可以預想,這里面肯定就是實際地訪問網絡的代碼了,這里面就是我們上面創建來的BasicNetWork來訪問網絡了:
@Override
public void run() {
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
while (true) {
long startTimeMs = SystemClock.elapsedRealtime();
Request<?> request;
try {
// Take a request from the queue.
//首先從網絡請求隊列中取出request對象我們這里是以StringRequest為例
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.
//判斷下request是不是是取消狀態
if (request.isCanceled()) {
request.finish("network-discard-cancelled");
request.notifyListenerResponseNotUsable();
continue;
}
addTrafficStatsTag(request);
// Perform the network request.
//這個地方的mNetwork就是BasicNetWork,調用BasicNetwork的performRequest方法來請求網絡
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();
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);
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();
}
}
}
這個方法跟上面的方法有點類似,這里主要有一個不同的地方是要調用mNetwork對象的performRequest方法,這個mNetwork就是BasicNetwork對象,所以我們看下BasicNetwork對象的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.
//得到一些另外的請求頭具體可以看getCacheHeaders方法
Map<String, String> additionalRequestHeaders =
getCacheHeaders(request.getCacheEntry());
//這里就是調用的AdaptedHttpStack的executeRequest來請求網絡(這里又包裝了HttpURLConnection和HttpClient訪問網絡)
httpResponse = mBaseHttpStack.executeRequest(request, additionalRequestHeaders);
int statusCode = httpResponse.getStatusCode();
//獲得到返回數據的頭部
responseHeaders = httpResponse.getHeaders();
// Handle cache validation.
if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
//如果返回的狀態碼是不修改的,那么則獲取緩存給NetworkResponse
Entry entry = request.getCacheEntry();
if (entry == null) {
return new NetworkResponse(HttpURLConnection.HTTP_NOT_MODIFIED, null, true,
SystemClock.elapsedRealtime() - requestStart, responseHeaders);
}
// Combine cached and response headers so the response will be complete.
List<Header> combinedHeaders = combineHeaders(responseHeaders, entry);
return new NetworkResponse(HttpURLConnection.HTTP_NOT_MODIFIED, entry.data,
true, SystemClock.elapsedRealtime() - requestStart, combinedHeaders);
}
// 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();
}
//不然就從網絡返回response取出返回的結果數據給NetworkResponse
return new NetworkResponse(statusCode, responseContents, false,
SystemClock.elapsedRealtime() - requestStart, responseHeaders);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
...........
}
}
}
我們看到這個地方主要就是調用mBaseHttpStack的executeRequest方法,這個mBaseHttpStack在構造BasicNetwork的時候會進行包裝:
@Deprecated
public BasicNetwork(HttpStack httpStack, ByteArrayPool pool) {
mHttpStack = httpStack;
mBaseHttpStack = new AdaptedHttpStack(httpStack);
mPool = pool;
}
可以看到這里的mBaseHttpStack就是AdaptedHttpStack對象,所以我們可以看到AdaptedHttpStack的executeRequest方法:
@Override
public HttpResponse executeRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
org.apache.http.HttpResponse apacheResp;
try {
//我們看到這個地方調用了mHttpStack的performRequest用來請求網絡
apacheResp = mHttpStack.performRequest(request, additionalHeaders);
} catch (ConnectTimeoutException e) {
// BasicNetwork won't know that this exception should be retried like a timeout, since
// it's an Apache-specific error, so wrap it in a standard timeout exception.
throw new SocketTimeoutException(e.getMessage());
}
//得到網絡返回的返回碼
int statusCode = apacheResp.getStatusLine().getStatusCode();
//返回所有的消息報頭
org.apache.http.Header[] headers = apacheResp.getAllHeaders();
List<Header> headerList = new ArrayList<>(headers.length);
for (org.apache.http.Header header : headers) {
headerList.add(new Header(header.getName(), header.getValue()));
}
if (apacheResp.getEntity() == null) {
//返回的數據為空則直接就返回狀態碼和頭部列表
return new HttpResponse(statusCode, headerList);
}
long contentLength = apacheResp.getEntity().getContentLength();
if ((int) contentLength != contentLength) {
throw new IOException("Response too large: " + contentLength);
}
//將所有的信息封裝成HttpResponse對象
return new HttpResponse(
statusCode,
headerList,
(int) apacheResp.getEntity().getContentLength(),
apacheResp.getEntity().getContent());
}
我們看到這里的主要代碼也就是調用mHttpStack的performRequest方法來請求網絡,這里的mHttpStack就是前面在構造newRequestQueue時候的HurlStack和HttpClientStack,這里面就是具體的HttpClient與HttpURLConnection訪問網絡的內容了,具體的這兩個怎么使用我們不細究。到這里我們網絡請求已經返回結果了。那么我們來看看我們是怎么發送我們返回結果到主線程的。
7.mDelivery postResponse(request, response)
我們看到前面已經網絡請求數據已經返回了,我們最后會解析完發送給主線程,我們馬上就來看看怎么發送的吧:
@Override
public void postResponse(Request<?> request, Response<?> response, Runnable runnable) {
request.markDelivered();
request.addMarker("post-response");
mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, runnable));
}
這里就是我們發送數據到主線程的代碼,我們看到這里會執行ResponseDeliveryRunnable線程,我們干脆就直接來看這個線程干了什么:
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返回結果,不然返回錯誤
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();
}
}
我看看到這個方法主要調用mRequest的deliverResponse來返回數據,這個mRequest我們是以StringRequest為例的,所以我們看StringRequest的deliverResponse方法:
@Override
protected void deliverResponse(String response) {
Response.Listener<String> listener;
synchronized (mLock) {
listener = mListener;
}
if (listener != null) {
listener.onResponse(response);
}
}
這個方法主要是調用的listener的onResponse進行回調的。所以就會調用到我們給StringRequest設置的listener了。到這里我們整個流程也就講完了,非常的通暢,跟前面的圖片完全符合呀。
總結:Volley的代碼非常清晰,思路也非常清晰,只要照著圖就能了解個大概,這篇是網絡框架的第一篇,希望我們接下來能把相應的網絡框架拿出來說說,大家一起努力。。。。