在NetworkDispatcher的run()方法中最后是由ExecutorDelivery对象将request 和response发送回主线程的。来看一看是怎么实现的。
ExecutorDelivery实现了ResponseDelivery 接口
package com.android.volley;
public interface ResponseDelivery {
/**
* Parses a response from the network or cache and delivers it.
*/
void postResponse(Request<?> request, Response<?> response);
/**
* Parses a response from the network or cache and delivers it. The provided
* Runnable will be executed after delivery.
*/
void postResponse(Request<?> request, Response<?> response, Runnable runnable);
/**
* Posts an error for the given request.
*/
void postError(Request<?> request, VolleyError error);
}
当在NetworkDispatcher类中执行mDelivery.postResponse(request, response);方法时,将request和response发送到ExecutorDelivery实现的postResponse(request, response)方法中。
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);
}
};
}
其中handler 是在RequestQueue的构造方法传入的new ExecutorDelivery(new Handler(Looper.getMainLooper()));主线程的Handler。mResponsePoster 为Executor接口的匿名类。查询jdk得到此接口左右如下:
/**
* 此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节、调度等)分离开来的方法。
* 通常使用 Executor 而不是显式地创建线程。
* 例如,可能会使用以下方法,而不是为一组任务中的每个任务调用 new Thread(new(RunnableTask())).start():
*/
意思就是不用为多个任务分别创建一个显式的线程了。重写的ResponseDelivery 接口方法如下:
@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));
}
postResponse方法被调用时,Executor的匿名类mResponsePoster将执行传递过来的 ResponseDeliveryRunnable对象,然后通过handler方法post回主线程。ResponseDeliveryRunnable实现了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;
}
// 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();
}
}
}
Handler post的实现runnable对象将会执行它,也就是会执行其run()方法。run()方法中,通过Request的方法将Response传递回去。以上就是实现过程。