OkHttp使用(四)拦截器
2017-03-06 17:16
274 查看
1、官方对拦截器的定义:
Interceptors area powerful mechanism that can monitor, rewrite, and retry calls.拦截器可以用来转换,重试,重写请求的机制。
2、两类拦截器(Interceptors)
拦截器的接口类:public interface Interceptor { Response intercept(Chain chain) throws IOException; interface Chain { Request request(); Response proceed(Request request) throws IOException; Connection connection(); } } |
1)应用拦截器(ApplicationInterceptors)
通过下面两种方式注册的为应用拦截器://方式一:在OkHttpClient.Builder中添加 new OkHttpClient.Builder().addInterceptor(interceptor) //方式二:在okHttpClient中直接添加 okHttpClient.interceptors().add(interceptor) |
class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); logger.info(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); logger.info(String.format("Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); return response; } } |
2)网络拦截器(Network Interceptors)
通过下面两种方式注册的为网络拦截器://方式一:在OkHttpClient.Builder中添加 new OkHttpClient.Builder().addNetworkInterceptor(interceptor) //方式二:在okHttpClient中直接添加 okHttpClient. networkInterceptors().add(interceptor) |
例如,如果你连接到一个支持压缩的网络服务器你可以使用一个应用拦截器来添加请求实体压缩。
/** This interceptor compresses the HTTP request body. Many webservers can't handle this! */ /** 这个拦截器压缩了请求实体. 很多网络服务器无法处理它 */ final class GzipRequestInterceptor implements Interceptor { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request originalRequest = chain.request(); if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) { return chain.proceed(originalRequest); } Request compressedRequest = originalRequest.newBuilder() .header("Content-Encoding", "gzip") .method(originalRequest.method(), gzip(originalRequest.body())) .build(); return chain.proceed(compressedRequest); } private RequestBody gzip(final RequestBody body) { return new RequestBody() { @Override public MediaType contentType() { return body.contentType(); } @Override public long contentLength() { return -1; // We don't know the compressed length in advance! } @Override public void writeTo(BufferedSink sink) throws IOException { BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); body.writeTo(gzipSink); gzipSink.close(); } }; } } |
3、拦截器选择
1)应用拦截器
不需要担心中间过程的响应,如重定向和重试.总是只调用一次,即使HTTP响应是从缓存中获取.
观察应用程序的初衷. 不关心OkHttp注入的头信息如:
If-None-Match.
允许短路而不调用
Chain.proceed(),即中止调用.
允许重试,使
Chain.proceed()调用多次.
2)网络拦截器
能够操作中间过程的响应,如重定向和重试.当网络短路而返回缓存响应时不被调用.
只观察在网络上传输的数据.
携带请求来访问连接.
相关文章推荐
- OKHttp使用拦截器(Interceptor)POST与GET方法,统一封装传公共参数
- OkHttp3 Interceptor 拦截器的使用 统一追加参数
- OkHttp使用(四)拦截器
- okhttp拦截器的使用
- OkHttp拦截器的简单使用
- OkHttp3拦截器的使用
- OkHttp基本使用(五)拦截器
- OkHttp的基本使用-3(添加拦截器和GET请求的参数拼接)
- 熟悉使用okhttp和拦截器使用
- Android OKHttp3拦截器的使用方法
- OkHttp中的拦截器使用--翻译自官方文档
- Rx系列学习笔记_使用okhttp拦截器统一处理返回请求
- OKHTTP拦截器的简单使用
- 使用MVP注册登录模块+封装的OKhttp,拦截器+QQ第三方登录+RecyclerView+SpringView上拉加载下拉刷新网络数据
- Struts2拦截器的使用
- 在struts中使用拦截器(Interceptor)控制登录和权限
- Struts2使用过滤器和拦截器进行简单权限校验
- CXF使用拦截器和XSLT转换消息内容
- Struts2拦截器的使用 (详解)
- 使用filter拦截器判断会话是否丢失