Android网络框架volley学习(十)分发结果ResponseDelivery简析
2018-01-29 10:47
447 查看
前面几篇文章讲解了一些请求响应等内容,本篇文章继续讨论关于结果分发的原理,这里面涉及到ResponseDelivery和ExecutorDelivery等类。
这是一个接口没什么好看的,主要是它的实现类,我们来解析一下ExecutorDelivery这个类。
另外,存在一个构造函数
它的一个参数是Handler,我们在RequestQueue类中,有关于它的构造方法,
可以看到,这里面ExecutorDelivery的构造方法传入了主线程的Looper,所以我们可以直接在响应结果返回中直接更新UI,这里面都给我们封装好了,不需要我们操作。
它的里面存在一个内部ResponseDeliveryRunnable,实现了runnable
它的run方法
这就是ResponseDelivery大概内容。
ResponseDelivery
ResponseDelivery类是一个接口,里面重载了三种方法。//从网络或者缓存response中解析并分发出去 void postResponse(Request<?> request, Response<?> response); //从网络或者缓存response中解析并分发出去,同时提供的runnable将会被执行 void postResponse(Request<?> request, Response<?> response, Runnable runnable); //error分发 void postError(Request<?> request, VolleyError error);
这是一个接口没什么好看的,主要是它的实现类,我们来解析一下ExecutorDelivery这个类。
ExecutorDelivery
ExecutorDelivery类实现了ResponseDelivery接口,它里面有一个变量Executor,用于在主线程中posting responses。/** 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() { @Override public void execute(Runnable command) { handler.post(command); } }; }
它的一个参数是Handler,我们在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 */ public RequestQueue(Cache cache, Network network, int threadPoolSize) { this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper()))); }
可以看到,这里面ExecutorDelivery的构造方法传入了主线程的Looper,所以我们可以直接在响应结果返回中直接更新UI,这里面都给我们封装好了,不需要我们操作。
它的里面存在一个内部ResponseDeliveryRunnable,实现了runnable
private final Request mRequest;//请求方式 private final Response mResponse;//返回结构 private final Runnable mRunnable;
它的run方法
// 如果请求被中断,那么就不需要发送响应了 if (mRequest.isCanceled()) { mRequest.finish("canceled-at-delivery"); return; } // 如果服务器响应成功,中途没有错误的发生 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 (mRunnable != null) { mRunnable.run(); }
这就是ResponseDelivery大概内容。
相关文章推荐
- Android网络框架volley学习(九)请求结果Response<T>简析
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比
- Android学习记录20171023--Volley网络框架(一)
- android网络框架volley学习之Volley类
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比 标签: androidVolleyOkhttpRetrofit网络请求框架
- android网络框架volley学习之HttpStack接口
- Android网络框架volley学习(八)缓存Cache简析
- Android 网络通信框架Volley学习
- android网络框架volley学习之RequestQueue类
- Android网络通信框架Volley的学习笔记
- Android网络框架volley学习(十一)volley源码解析总结
- Android学习记录之Volley网络通信框架基础解析(1)
- android Volley网络通信框架学习
- Android网络框架volley学习(五)请求队列RequestQueue简析
- Android网络框架volley学习(六)调度器CacheDispatcher简析
- Android网络框架volley学习(二)工作流程分析
- Android中Http网络请求库框架Volley和Asnyc-http的使用---第三方库学习笔记(一)
- android网络框架volley学习之基本数据类
- Android网络框架volley学习(一)基本用法