13年的google的I/O大会上,google发布了Volley,适用于数量多但是数据量不大的网络请求框架,虽然google早已经不维护了,但是其中的代码风格和设计风格都是值得我们学习的
简单说一下流程:
首先通过Volley的静态方法newRequestQueue()初始化一些参数,包括直接发起请求的HttpStack对象,通过HttpStack对象作为参数间接对请求加以处理的NetWork对象,一个本地的缓存对象CaChe,以及以CaChe和NetWork为参数的我们直接操纵的请求队列RequestQueue。
在RequestQueue的构造器中,除了上面的两个参数,还有一个处理发送请求响应的ResponseDelivery对象,以及一个数量为4的处理网络请求的线程池NetworkDispatcher调度器,还有两个个成员变量网络请求队列和缓存队列,均为PriorityBlockingQueue类型。
以RequestQueue.start()作为开始,创建4个网络请求对象NetworkDispatcher以及一个请求缓存对象CacheDispatcher,两者均继承于Thread对象,内部都写有一个死循环,用户不断地从网络请求队列PriorityBlockingQueue拿出或是加入换粗队列之中,最后将从网络请求队列中的请求通过NetWork进行网络通信,然后将响应通过ResponseDelivery发送出去,用于我们接收处理。因为Request是面向接口编程,对于Request我们可以自己定制。
Part1 RequestQueue的初始操作:
通常情况下只调用参数为Context的newRequestQueue方法即可:
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context) {
return newRequestQueue(context, (BaseHttpStack) null);
}
因为最后都会调用到:
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
* @deprecated Use {@link #newRequestQueue(Context, BaseHttpStack)} instead to avoid depending
* on Apache HTTP. This method may be removed in a future release of Volley.
*/
@Deprecated
@SuppressWarnings("deprecation")
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
if (stack == null) {
return newRequestQueue(context, (BaseHttpStack) null);
}
return newRequestQueue(context, new BasicNetwork(stack));
}
其中,因为httpClient和HttpUrlConnection的原因,在初始化HttpStack的时候会有一些出入
/**
* Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack A {@link BaseHttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/
public static RequestQueue newRequestQueue(Context context, BaseHttpStack stack) {
BasicNetwork network;
if (stack == null) {
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 {
network = new BasicNetwork(stack);
}
return newRequestQueue(context, network);
}
因为在api<9之前只有httpclient,但是使用难度太大,所以谷歌在api=9的时候重新实现了一个网络请求类HttpURLConnection,虽然还有些问题,但是相对于谷歌现在已经放弃对httpClient的维护了,所以大家可以尽情的使用,有兴趣的同学可自行百度拓展,限于篇幅和本章侧重点就不多说了。
所以这段代码主要是声明了网络请求的实现类BasicNetWork。
我们进入HttpClientStack,可以看到其实现了HttpStack接口,以及一个HttpClient对象
/**
* An HttpStack that performs request over an {@link HttpClient}.
*
* @deprecated The Apache HTTP library on Android is deprecated. Use {@link HurlStack} or another
* {@link BaseHttpStack} implementation.
*/
@Deprecated
public class HttpClientStack implements HttpStack {
protected final HttpClient mClient;
进入HUrlStack 我们可以看到其最终也是实现了HttpStack接口:
/**
* An {@link HttpStack} based on {@link HttpURLConnection}.
*/
public class HurlStack extends BaseHttpStack {
/** An HTTP stack abstraction. */
@SuppressWarnings("deprecation") // for HttpStack
public abstract class BaseHttpStack implements HttpStack {
在HttpStack中我们可以看到 只有一个抽象方法performRequest(),参数为Request类型的请求和一个Map类型的请求头:
/**
* An HTTP stack abstraction.
*
* @deprecated This interface should be avoided as it depends on the deprecated Apache HTTP library.
* Use {@link BaseHttpStack} to avoid this dependency. This class may be removed in a future
* release of Volley.
*/
@Deprecated
public interface HttpStack {
/**
* Performs an HTTP request with the given parameters.
*
* <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
* and the Content-Type header is set to request.getPostBodyContentType().</p>
*
* @param request the request to perform
* @param additionalHeaders additional headers to be sent together with
* {@link Request#getHeaders()}
* @return the HTTP response
*/
HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError;
}
接着往下看,在performRequest()中我们看到了httpclient调用execute发起了请求
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
addHeaders(httpRequest, additionalHeaders);
addHeaders(httpRequest, request.getHeaders());
onPrepareRequest(httpRequest);
HttpParams httpParams = httpRequest.getParams();
int timeoutMs = request.getTimeoutMs();
// TODO: Reevaluate this connection timeout based on more wide-scale
// data collection and possibly different for wifi vs. 3G.
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
return mClient.execute(httpRequest);
}
所以我们肯定在HUrlStack和HttpClientStack中的performRequest则是具体的网络请求,一个是用HttpUrlConnection,一个则是用HttpClient
HttpClientStack中的请求方法
@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders);
addHeaders(httpRequest, additionalHeaders);
addHeaders(httpRequest, request.getHeaders());
onPrepareRequest(httpRequest);
HttpParams httpParams = httpRequest.getParams();
int timeoutMs = request.getTimeoutMs();
// TODO: Reevaluate this connection timeout based on more wide-scale
// data collection and possibly different for wifi vs. 3G.
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
return mClient.execute(httpRequest);
}
因为在BaseHttpStack中将perfornRequest废弃掉了,重新new了一个抽象executeRequest()
/** An HTTP stack abstraction. */
@SuppressWarnings("deprecation") // for HttpStack
public abstract class BaseHttpStack implements HttpStack {
/**
* Performs an HTTP request with the given parameters.
*
* <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
* and the Content-Type header is set to request.getPostBodyContentType().
*
* @param request the request to perform
* @param additionalHeaders additional headers to be sent together with
* {@link Request#getHeaders()}
* @return the {@link HttpResponse}
* @throws SocketTimeoutException if the request times out
* @throws IOException if another I/O error occurs during the request
* @throws AuthFailureError if an authentication failure occurs during the request
*/
public abstract HttpResponse executeRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError;
/**
* @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated
* Apache HTTP library. Nothing in Volley's own source calls this method. However, since
* {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation in
* case legacy client apps are dependent on that field. This method may be removed in a future
* release of Volley.
*/
@Deprecated
@Override
public final org.apache.http.HttpResponse performRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
....
....
return apacheResponse;
}
所以在HUrlStack中的具体实现则是:
@Override
public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
String url = request.getUrl();
HashMap<String, String> map = new HashMap<>();
map.putAll(request.getHeaders());
map.putAll(additionalHeaders);
if (mUrlRewriter != null) {
String rewritten = mUrlRewriter.rewriteUrl(url);
if (rewritten == null) {
throw new IOException("URL blocked by rewriter: " + url);
}
url = rewritten;
}
URL parsedUrl = new URL(url);
HttpURLConnection connection = openConnection(parsedUrl, request);
for (String headerName : map.keySet()) {
connection.addRequestProperty(headerName, map.get(headerName));
}
setConnectionParametersForRequest(connection, request);
// Initialize HttpResponse with data from the HttpURLConnection.
int responseCode = connection.getResponseCode();
if (responseCode == -1) {
// -1 is returned by getResponseCode() if the response code could not be retrieved.
// Signal to the caller that something was wrong with the connection.
throw new IOException("Could not retrieve response code from HttpUrlConnection.");
}
if (!hasResponseBody(request.getMethod(), responseCode)) {
return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()));
}
return new HttpResponse(responseCode, convertHeaders(connection.getHeaderFields()),
connection.getContentLength(), inputStreamFromConnection(connection));
}
回到volley,我们看下RequestQueue的构造器:
/**
* Creates the worker pool. Processing will not begin until {@link #start()} is called.
*
* @param cache A Cache to use for persisting responses to disk
* @param network A Network interface for performing HTTP requests
* @param threadPoolSize Number of network dispatcher threads to create
* @param delivery A ResponseDelivery interface for posting responses and errors
*/
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}
Cache和NetWrok使我们在volley中实现的对象,第三个是线程的数量,volley默认的4,当然也可以手动设置,第四个参数则是处理响应的对象,可以看下实现:
/**
* Creates the worker pool. Processing will not begin until {@link #start()} is called.
*
* @param cache A Cache to use for persisting responses to disk
* @param network A Network interface for performing HTTP requests
* @param threadPoolSize Number of network dispatcher threads to create
*/
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
可以看到,在实例化ExecutorDelivery的时候,包含有一个主线程Looper的Hnalder对象,开头有说过,delivery是将我们的请求响应进行处理,带着这一目标我们进入ExecutorDelivery类看看:
/**
* Creates a new response delivery interface.
* @param handler {@link Handler} to post responses on
*/
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);
}
};
}
在它的构造器里可以看到实现了一个Executor对象,并将其Runnable作为参数,在主线程中去进行操作,那我们可以简单推测一下,这个Runnable里面应该就是包含着响应的处理任务:
@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));
}
@Override
public void postError(Request<?> request, VolleyError error) {
request.addMarker("post-error");
Response<?> response = Response.error(error);
mResponsePoster.execute(new ResponseDeliveryRunnable(request, response, null));
}
这三个是方法是继承于ExecuteDelivery的父类,里面的代码是将一个Runnable对象通过mResponsePoster发送到主线程,这个mResponsePoster就是我们在构造器里面实现的对象,我们再一起去看看Runnable的实现类 ResponseDeliveryRunnable:
/**
* A Runnable used for delivering network responses to a listener on the
* main thread.
*/
@SuppressWarnings("rawtypes")
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() {
// 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();
}
}
}
run()里面的意思很明显 如果response成功了 就通过request发送一个成功的回调,如果失败了就通过request发送一个失败的回调,并结束掉Request。
我们来看一下这两个回调,以StringRequest为例:
@Override
protected void deliverResponse(String response) {
Response.Listener<String> listener;
synchronized (mLock) {
listener = mListener;
}
if (listener != null) {
listener.onResponse(response);
}
}
在StringRequest中只是实现了成功的回调,失败的回调在Request父类之中,
mListerner则是我们在最初实例化StringRequest的时候手动传进来的参数,然后通过这个监听调用onResponse方法,并将我们的响应结果放进去,因为这里是一StringRequest为例,所以Listener类型是String类型的,这样我们就可以直接拿到响应结果了,失败的回调一样,我们再来回顾下我们通常情况下使用Volley的写法:
//1、创建请求队列
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
/**2、实例化StringRequest请求
* 第一个参数选择Request.Method.GET即get请求方式
* 第二个参数的url地址根据文档所给
* 第三个参数Response.Listener 请求成功的回调
* 第四个参数Response.ErrorListener 请求失败的回调
*/
StringRequest stringRequest = new StringRequest(Request.Method.GET,"https:xxx",
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//String s即为服务器返回的数据
Log.d("cylog", s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("cylog", volleyError.getMessage(),volleyError);
}
});
//3、将请求添加进请求队列
requestQueue.add(stringRequest);
到现在,ExecutorDelivery发送请求响应这条路算是打通了。
Part2 Volley的运作
我们在初始化并拿到RequestQueue之后,通过ReqeustQueue.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.
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();
}
}
/**
* Stops the cache and network dispatchers.
*/
public void stop() {
if (mCacheDispatcher != null) {
mCacheDispatcher.quit();
}
for (final NetworkDispatcher mDispatcher : mDispatchers) {
if (mDispatcher != null) {
mDispatcher.quit();
}
}
}
在正式运作之前,先要确保所有的请求调度器已停止运作,所以也就要求我们RequestQueue需要全局只有一个。
start()只做了两件事,创建一个Thread类型的缓存网络调度器,以及创建四个Thread类型的网络调度器。
我们先来看下NetworkDispatcher 的run方法:
@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) {
return;
}
}
}
}
里面有一个死循环,不停的调用processRequest方法,再来看下processRequest
// Extracted to its own method to ensure locals have a constrained liveness scope by the GC.
// This is needed to avoid keeping previous request references alive for an indeterminate amount
// of time. Update consumer-proguard-rules.pro when modifying this. See also
// https://github.com/google/volley/issues/114
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.
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();
}
}
这个方法首先是从缓存网络队列中拿出一个请求,如果这个请求已经被取消,则将这个请求finish掉,并加一个一个标识,然后退出本次循环,继续从队列中取出下一个请求。如果这个请求没有被取消,则通过NetWork发送请求,进行网络通信,并返回一个NetWorkResponse类型的响应:
// Perform the network request.
NetworkResponse networkResponse = mNetwork.performRequest(request);
然后将这个响应通过request的parseNetworkResponse方法进行处理,parseNetworkResponse方法是每个request子类必须要实现的方法,用以将响应封装成我们想要的格式,比如String,比如Json等等,最后返回一个Response<?>类型的对象,然后加一个标识。
如果这个请求允许被缓存,并且响应的Cache.Entity不为空的话就将这个请求的url为key,Cache.Entity为Value存储在本地缓存中。
然后我们再去看下NetworkDispatcher:
@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) {
return;
}
}
}
}
同样的,它也继承于Thread,并且在run里面也是一个死循环,不停的调用processRequest()。不同的是,缓存调度器里面的是一直不断从缓存队列中读取请求,网络调度器是不断的从网络队列中读取请求:
final Request<?> request = mCacheQueue.take();
Request<?> request = mQueue.take();
ok,我们再来看看请求的添加,也就是RequestQueue的.add方法:
/**
* Adds a Request to the dispatch queue.
* @param request The request to service
* @return The passed-in request
*/
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;
}
逻辑很简单,如果请求可以被缓存,就将缓存存放到缓存队列之中,如果不可以被缓存,则放入到网络请求队列之中。
至此,Volley的流程是走完了。