OkHttp3 Interceptor 拦截器的使用 统一追加参数
来源:互联网 发布:淘宝网抓绒布料 编辑:程序博客网 时间:2024/06/04 20:14
日志拦截器
- 添加依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.3.0'
- 设置
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(httpLoggingInterceptor);
Get请求统一追加参数
public class AppendUrlParamInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); //拿到有信息的buidler HttpUrl.Builder builder = request.url().newBuilder(); //得到新的Url(已经追加好了参数) HttpUrl newUrl = builder .addQueryParameter("token", "iamtoken") .build(); //利用新的Url 构建新的Request ,并发送给服务器 Request newRequest = request.newBuilder() .url(newUrl) .build(); return chain.proceed(newRequest); }}
Header统一追加参数
public class AppendHeaderParamInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); //核心也是通过newBuilder 拿到Builder Headers.Builder builder = request. headers(). newBuilder(); //统一追加header参数 Headers newHeader = builder .add("header1", "iam header1") .build(); Request newRequest = request.newBuilder() .headers(newHeader) .build(); return chain.proceed(newRequest); }}
Post请求body统一追加参数
public class AppenBodyParamsInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); //构建新Body RequestBody newBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded") , "token=iamtoken"); Request newRequest = request.newBuilder() .post(newBody) .build(); return chain.proceed(newRequest); }}
阅读全文
0 0
- OkHttp3 Interceptor 拦截器的使用 统一追加参数
- OKHttp使用拦截器(Interceptor)POST与GET方法,统一封装传公共参数
- OkHttp3拦截器的使用
- 拦截器Interceptor的使用
- retrofit2.0使用拦截器Interceptor统一打印请求与响应的json
- retrofit2.0使用拦截器Interceptor统一打印请求与响应的json
- android,retrofit,okhttp,日志拦截器,使用拦截器Interceptor统一打印请求与响应的json
- OkHttp3中Interceptor的使用心得
- CXF拦截器(Interceptor)的使用
- Struts 拦截器interceptor 的使用
- Struts2中Interceptor拦截器的使用
- springMVC中Interceptor拦截器的使用
- jfinal中Interceptor拦截器的使用
- Struts2 Interceptor 拦截器的使用流程
- Hibernate 拦截器Interceptor使用
- springMVC使用interceptor拦截器
- okhttp3缓存设置及拦截器的使用
- SpringMVC中使用Interceptor拦截器的使用
- 织梦日期函数
- QT多线程(一)
- 安卓手机无法root的情况下使用busybox
- 数据结构C语言版--单链表的基本功能实现
- UVa1368 DNA Consensus string AC
- OkHttp3 Interceptor 拦截器的使用 统一追加参数
- 神经网络和CNN的一些基础概念,基础知识的梳理
- Java OOP之多态
- 线程
- Q-Prime Ring Problem
- 判断一个div是否在屏幕外
- How To Use Google Logging Library (glog)
- HTML基础知识
- Settimeout 设置以后,可以用cleartimeout清除。