Android中的volley_9_ResponseDelivery和ExecutorDelivery

来源:互联网 发布:企业数据库的建立 编辑:程序博客网 时间:2024/04/27 20:17

ResponseDelivery定义为一个接口,这是volley中最为有特色的定义方式,无论是Request Network还是HttpStack,每一个小模块都是用一个接口来对接的,大大降低了模块之间的耦合性,同时由于模块之间对接是接口,这样扩展性极佳。

ResponseDelivery,从字面也可以理解,其主要的作用就是传递响应。内部定义了三个方法:

/**     * Parses a response from the network or cache and delivers it.     *      * 此方法用于传递请求结果,request 和 response 参数分别表示请求信息和返回结果信息     */    public 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.     *      * 正常传递完请求和响应结果后立刻执行runnable     */    public void postResponse(Request<?> request, Response<?> response, Runnable runnable);    /**     * Posts an error for the given request.     *      * 传输请求错误     */    public void postError(Request<?> request, VolleyError error);

ExecutorDeliveryResponseDelivery的实现类,也是volley中默认的响应传递类。在RequestQueue内的构造方法就是使用了ExecutorDelivery。

public RequestQueue(Cache cache, Network network, int threadPoolSize) {this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper())));}
在该类中定义了一个Executor,其主要做用就是向主线程中传递响应。

/** Used for posting responses, typically to the main thread. 向主线程中传递响应 */private final Executor mResponsePoster;/** * 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() {@Overridepublic void execute(Runnable command) {handler.post(command);}};}
从构造方法可以看出来,传入了一个handler,而这个handler在RequestQueue内是通过主线程的Looper来创建的


在ExecutorDelivery内有一个非常重要的方法,该方法的主要作用就是标记一个request已经被传递了响应,并在runnable接口的匿名内部类中对request进行了大量的操作。

@Overridepublic void postResponse(Request<?> request, Response<?> response,Runnable runnable) {//标记该请求已经被传递request.markDelivered();request.addMarker("post-response");mResponsePoster.execute(new ResponseDeliveryRunnable(request, response,runnable));}

其中ResponseDeliveryRunnable实现了接口Runnable,该类的构造方法中有三个参数:

/** * 待传递响应的请求 */private final Request mRequest;/** * 拿回来的响应(从Cache或网络) */private final Response mResponse;/** * 分发完后需要立刻执行的操作 ,可以为null */private final Runnable mRunnable;public ResponseDeliveryRunnable(Request request, Response response,Runnable runnable) {mRequest = request;mResponse = response;mRunnable = runnable;}

需要注意的是runnable,这个也是从外面传递进来的,其主要作用就是,当一传递完响应立刻要执行的操作,该操作是一个回调方法,需要响应传递调用者自己去实现。

在该类的run方法内对request进行了大量操作:

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()) {// 就分发响应,该方法是一个抽象方法,参数是响应的JavaBean,需要子类去具体操作该beanmRequest.deliverResponse(mResponse.result);} else {// error不为空,也就是响应错误 分发错误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.// 如果想要在分发完响应后立刻执行一个操作,传递进来runnable 不为空表示就执行runnable 不想执行操作// 传进来null即可if (mRunnable != null) {mRunnable.run();}}

之,传递响应,其实就是对request进行的操作,包括finish掉request、给request传递缓存数据等。


demo下载:http://download.csdn.net/detail/vvzhouruifeng/8747599

1 0