标题:
retrofit2.0+OkHttpClient 中新增公共参数get/post及网络请求日志打印
[打印本页]
作者:
look_w
时间:
2019-3-3 18:59
标题:
retrofit2.0+OkHttpClient 中新增公共参数get/post及网络请求日志打印
需求1:更加统一方便管理公共参数
需求2:方便调试http请求日志,查看请求参数和返回参数
新增一个网络拦截器,代码如下:
LogInterceptor 类:
public class LogInterceptor implements Interceptor {
public static String TAG = "LogInterceptor";
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request oldRequest = chain.request();
Request.Builder newRequestBuild;
String method = oldRequest.method();
String postBodyString="";
if("POST".equals(method)){
FormBody.Builder formBodyBuilder = new FormBody.Builder();
formBodyBuilder.add("deviceOs", iCommon.DEVICE_OS);
formBodyBuilder.add("appVersion", Utils.instance().getAppVersionName());
newRequestBuild = oldRequest.newBuilder();
RequestBody formBody = formBodyBuilder.build();
postBodyString = bodyToString(oldRequest.body());
postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody);
newRequestBuild.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString));
}else {
// 添加新的参数
HttpUrl.Builder commonParamsUrlBuilder = oldRequest.url()
.newBuilder()
.scheme(oldRequest.url().scheme())
.host(oldRequest.url().host())
.addQueryParameter("deviceOs", iCommon.DEVICE_OS)
.addQueryParameter("appVersion", Utils.instance().getAppVersionName());
newRequestBuild = oldRequest.newBuilder()
.method(oldRequest.method(), oldRequest.body())
.url(commonParamsUrlBuilder.build());
}
Request newRequest = newRequestBuild
.addHeader("Accept", "application/json")
.addHeader("Accept-Language", "zh")
.build();
long startTime = System.currentTimeMillis();
okhttp3.Response response = chain.proceed(newRequest);
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
okhttp3.MediaType mediaType = response.body().contentType();
String content = response.body().string();
int httpStatus = response.code();
StringBuilder logSB = new StringBuilder();
logSB.append("-------start:"+method+"|");
logSB.append(newRequest.toString()+"\n|");
logSB.append(method.equalsIgnoreCase("POST")?"post参数{"+ postBodyString +"}\n|":"");
logSB.append("httpCode=" + httpStatus + ";Response:" + content+"\n|");
logSB.append("----------End:" + duration + "毫秒----------");
Log.d(TAG,logSB.toString());
return response.newBuilder()
.body(okhttp3.ResponseBody.create(mediaType, content))
.build();
}
private static String bodyToString(final RequestBody request) {
try {
final RequestBody copy = request;
final Buffer buffer = new Buffer();
if (copy != null)
copy.writeTo(buffer);
else
return "";
return buffer.readUtf8();
} catch (final IOException e) {
return "did not work";
}
}
}
使用方法:
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.retryOnConnectionFailure(false)
.cookieJar(s_cookieJar)
.connectTimeout(DEFAULT_NETWORK_TIMEOUT, TimeUnit.MILLISECONDS)
.readTimeout(DEFAULT_NETWORK_TIMEOUT, TimeUnit.MILLISECONDS)
.writeTimeout(DEFAULT_NETWORK_TIMEOUT, TimeUnit.MILLISECONDS)
.addInterceptor(new LogInterceptor());
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0