Okhttp的拦截器
来源:互联网 发布:网络视频广告数据分析 编辑:程序博客网 时间:2024/05/29 23:24
import android.util.Log;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
/**
* Author:kson
* E-mail:19655910@qq.com
* Time:2017/10/18
* Description:
*/
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();
}
}
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
/**
* Author:kson
* E-mail:19655910@qq.com
* Time:2017/10/18
* Description:
*/
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();
}
}
阅读全文
0 0
- Okhttp的拦截器
- okhttp拦截器的使用
- 关于OKhttp的拦截器
- okhttp拦截器的实现
- okhttp拦截器的实现
- OkHttp的拦截器Interceptor
- okHttp拦截器的使用
- OKHTTP的拦截器以及https访问
- okhttp的interceptor拦截器功能
- 一起来写OKHttp的拦截器
- 浅析 OkHttp 的拦截器机制
- OKHTTP拦截器的简单使用
- OkHttp拦截器的简单使用
- OkHttp的拦截器机制分析
- Android Okhttp的网络拦截器
- OKhttp的简单封装加拦截器
- okHttp的封装和拦截器
- 关于okhttp的自定义拦截器
- leetcode 657 Judge Route Circle
- 新手应该知道的 26 条 linux 命令
- spark安装与配置
- java爬虫小项目-挖取CSDN博客文章
- 3D NAND要开始全面收割存储市场了?
- Okhttp的拦截器
- 增强式for循坏
- 数据库触发器
- Inception V1
- hibernate get() load()
- odoo自定义报表
- AliOS Things云端物联网操作系统
- FeignClient相关
- 异常捕获机制