前面几篇通过源码分析了Volley是怎样进行请求调度及请求是如何被实际执行的,这篇最后来看下请求结果是如何交付给请求者的(一般是Android的UI主线程)。
类图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2YAAAFeCAIAAABhAZtAAAAgAElEQVR4nO2d69GrKhhGqSclWEIKcVJGZtKAFXxlOLP/nT4sJueHNy4viFERZa15Zk/2Gy9IUJ4PBNQXAAAAACCIOjsBAAAAAJA7WEYAAAAAWADLCAAAAAALYBkBAAAAYAEsIwAAAAAsgGUEAAAAgAUMy/jff/+JGxEnTpw4ceLEiRMvOU4rIwAAAAAsgGUEAAAAgAWwjAAAAACwAJYRAAAAABbAMgIAAADAAlhGAAAAAFgAywgAAAAACzAvY6bxt1IIXV2n30fEiRMnTnyvOK2MmfJW6vv9h9B1FXCNAABwOXigZwqWEV1dfRnGNQIA3AOe5pmCZURX11SGcY0AADeAR3mmYBnR1aWXYVwjAMDV4TmeKaJlVFpQmYjbWFsGDiVu7yPmIKL6bcQUihtHbhnOqMgTRR45kGk/p2rx2x/OlYOsMoxrBAC4NDzEM8VnGXVz436wggEnFLaMkc7GZ2EX9xXP6DOmP7ilxTQEThRjl3/wsls85eIPtJjgU+SWYVwjAMB14QmeKW51G3CKPssY6U5+s4wB77joY0T7NZk561CRx4m/hPAGi1/5rkKPiJ/De8Xkz+I24V82sd7S2d+4RgCAa2I8vvOcB6jM+Ntj43xOQnQtkZ5JOecKWBDr4FYk/qTi5YQPtXjwsPda3HjxLG7CwpHFC1m0dIum8IdrT6a35+xvj2vM4b4jTpw4ceK+OH/xZ4pb3UZaAcvM+XYJWBCf6bH+DR/TTYNogKxvA2n2XdfiFfnOtWjO3LO4+/oywf03fC5xs1X5HPPLJtbbf/Y3bY0AAFeDB3emuNVt2MO5Dkb0JaL18TkSn/EKuJPwoRbPGDA6YZ+0mLBANvou0PchZi/xX9ewxuS8FffZZR9uTibT+6e1YQAAIE94ameKaBktK+A6gxhfErA1AeMSNlKufVk0Uot+yL0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iIs40GQrQAAUARYxkKEZTwIshUAAIoAy1iIsIwHQbYCAEARYBkLEZbxIJiXkThx4sSJFxHHMhYi5mU8KI4TBwCAIngrhQrR2WXtnpCtAAAAALAAlhEAAAAAFsAyAgAAAMACWEYAAAAAWADLCAAAAAALYBkBAAAAYAHmZSROnDhx4sSJEyfOvIwAAAAAsA0sIwAAAAAsgGUEAAAAgAWwjAAAAACwAJYRAAAAABbAMgIAAADAAlhGAAAAAFiAeRmJEydOnDhx4sSJMy8jAAAAAGwDywgAAAAAC2AZAQAAAGABLCMAAAAALIBlBAAAAIAFsIwAAAAAsMD/rLZjv+L3TywAAAAASUVORK5CYII=" alt="" />
请求结果的交付是通过ResponseDelivery接口完成的,它有一个实现类ExecutorDelivery, 主要有postResponse()与postError()两个方法,分别在请求成功或失败时将结果提交给请求发起者。
1. 首先,在NetworkDispatcher的run()方法中,当服务器返回响应并解析完后,会调用mDelivery.postResponse(request, response);来提交请求响应。
@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;
} 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");
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));
}
}
}
2. 看ExecutorDelivery中postResponse()方法的具体实现。其中mResponsePoster是一个Executor。每post一个response,都会调用ResponseDeliveryRunnable的run()方法。在这个run()方法中,会通过mRequest.deliverResponse(mResponse.result)来传递response的result,这个result其实就是已经解析好的响应结果,比如一个表示处理结果的字符串或一个User对象。
@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));
} /**
* 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() {
// If this request has canceled, finish it and don't deliver.
if (mRequest.isCanceled()) {
mRequest.finish("canceled-at-delivery");
return;
} 38 // Deliver a normal response or error, depending.
39 if (mResponse.isSuccess()) {
40 mRequest.deliverResponse(mResponse.result);
41 } else {
42 mRequest.deliverError(mResponse.error);
43 } // 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();
}
}
}
3. 既然是通过Request的deliverResponse()来传递响应结果,就来看下这个方法, 第二篇中已经知道这个方法是个抽象函数,由它子类来实现。以第一篇中的MyGsonRequest为例,其实现很简单,就是调用了mListener的onResponse方法。
@Override
protected void deliverResponse(T response) {
mListener.onResponse(response);
}
这个mListener就是在主线程实例化MyGsonRequest的时候,传过来的一个Response.Listener<T>实例,这是MyGsonRequest的构造函数:
public MyGsonRequest(int method
, String url
, Object requestBody
, Class<T> responseClass
, Listener<T> listener
, ErrorListener errorListener) { super(method, url, errorListener);
this.mRequestBody = requestBody;
this.mResponseClass = responseClass;
this.mListener = listener;
mGson = new Gson(); }
这里mListener也就是第一篇中在主线程中通过createRegisterSuccessListener函数返回的监听器实例,如下代码所示。 所以最终会调到这里的onResponse()方法,来做一些更新UI或提示用户请求成功之类的操作。请求失败时,响应错误结果的提交与之类似。这样,Volley就完成了响应结果的交付。
private Listener<String> createRegisterSuccessListener() {
return new Listener<String>() {
@Override
public void onResponse(String response) {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
Toast.makeText(
RegisterActivity.this,
getString(R.string.msg_register_success),
Toast.LENGTH_SHORT).show(); }
};
}
这里还有一个问题, 因为更新UI的操作只能在主线程中进行,那么ResponseDeliveryRunnable的run()方法不能再新起一个线程来执行,而应该在主线程中执行,这个是如何做到的?
其实还是用的Handler,Looper,MessageQueue的那套机制。 在Volley初始化一个RequestQueue的时候,会调用RequestQueue的如下构造函数,它构建了一个ExecutorDelivery对象,并把一个与主线程的Looper关联的一个Handler,
public RequestQueue(Cache cache, Network network, int threadPoolSize) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())));
}
然后再看下ExecutorDelivery的构造方法, 通过handler的post方法,把ResponseDeliveryRunnable 这个runnable加到了主线程的消息队列中,所以它的run()方法是在主线程中执行的。
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);
}
};
}
到这里,Volley的源码基本上看完了。 还有一些诸如NetworkImageView控件(这个挺好用,直接可以取代ImageView),以及Cache的一些实现,有时间再具体看看。