Okhttp的拦截器
2017-10-21 12:57
211 查看
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();
}
}
相关文章推荐
- OKHTTP的拦截器以及https访问
- okhttp拦截器的实现
- Okhttp 拦截器
- 拦截器OKhttp
- OkHttp封装,以及拦截器,拦截公共参数
- OkHttp基本使用(五)拦截器
- okHttp的日志拦截器
- OkHttp使用(四)拦截器
- Android OkHttp官方Wiki之Interceptors拦截器
- OkHttp完全解析(六)拦截器
- Android的OkHttp包中的HTTP拦截器Interceptor用法示例
- OkHttp拦截器
- OKhttp的简单封装加拦截器
- 添加okhttp拦截器
- OkHttp之拦截器
- okhttp3Util工具类二次封装//及拦截器添加公共参数
- OkHttp的get,post请求,拦截器
- Android 封装Okhttp+Retrofit+RxJava,外加拦截器实例
- Retrofit 中设置 OkHttp 拦截器,保存和添加 Cookie
- OkHttp3源码解析02-拦截器