okhttp添加拦截器
来源:互联网 发布:python矩阵求和 编辑:程序博客网 时间:2024/05/29 23:23
public class LogInterceptor implements Interceptor { public static String TAG = "LogInterceptor"; @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); long startTime = System.currentTimeMillis(); okhttp3.Response response = chain.proceed(chain.request()); long endTime = System.currentTimeMillis(); long duration=endTime-startTime; okhttp3.MediaType mediaType = response.body().contentType(); String content = response.body().string(); Log.d(TAG,"\n"); Log.d(TAG,"----------Start----------------"); Log.d(TAG, "| "+request.toString()); String method=request.method(); if("POST".equals(method)){ StringBuilder sb = new StringBuilder(); if (request.body() instanceof FormBody) { FormBody body = (FormBody) request.body(); for (int i = 0; i < body.size(); i++) { sb.append(body.encodedName(i) + "=" + body.encodedValue(i) + ","); } sb.delete(sb.length() - 1, sb.length()); Log.d(TAG, "| RequestParams:{"+sb.toString()+"}"); } } Log.d(TAG, "| Response:" + content); Log.d(TAG,"----------End:"+duration+"毫秒----------"); return response.newBuilder() .body(okhttp3.ResponseBody.create(mediaType, content)) .build(); }}
在 okhttp里面这样添加
OkHttpClient okHttpClient = new OkHttpClient.Builder(). addInterceptor(new LogInterceptor()).build();
这样就OK了
阅读全文
0 0
- okhttp添加拦截器
- 添加okhttp拦截器
- OkHttp拦截器POST添加公共参数
- okhttp拦截器添加请求头
- 自定义OKhttp拦截器 添加请求头
- Retrofit 中设置 OkHttp 拦截器,保存和添加 Cookie
- android给retrofit 的 添加okhttp的拦截器
- Okhttp 拦截器
- okhttp 拦截器调用。
- OkHttp-Interceptors拦截器
- okhttp拦截器+Imageloader
- OkHttp拦截器
- okhttp拦截器
- Okhttp的拦截器
- okhttp应用拦截器
- okhttp拦截器
- Okhttp网络拦截器
- okhttp网络拦截器
- 模拟题
- better-scroll的那些坑
- 校长推荐:50个网安从业者可能会关注公众号
- 微信小程序开发笔记及心得(持续更新)
- 图片滑动验证码js插件
- okhttp添加拦截器
- eclipse启动出现“An Error has Occurred. See the log file”解决方法
- Markdown实用语法
- 云端还是内部?有可能是两者兼备
- 枚举一定条件的子集
- 通过Callable接口实现多线程
- 前端冷知识
- SylixOS调试方法详解——综合案例分析
- 浮点数加减乘方法封装~