Skip to content

Commit 25b42be

Browse files
committed
优化 LogInterceptor
1 parent 082f82b commit 25b42be

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

mvvmframe/src/main/java/com/king/frame/mvvmframe/http/interceptor/LogInterceptor.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import androidx.annotation.NonNull;
88
import okhttp3.Interceptor;
9+
import okhttp3.MediaType;
910
import okhttp3.Request;
1011
import okhttp3.RequestBody;
1112
import okhttp3.Response;
@@ -24,26 +25,32 @@
2425
*/
2526
public class LogInterceptor implements Interceptor {
2627

28+
private static final String multipartType = "multipart";
2729
private final static Charset UTF_8 = Charset.forName("UTF-8");
2830

2931
@Override
3032
public Response intercept(Chain chain) throws IOException {
3133
Request request = chain.request();
32-
Timber.i(String.format("%1$s -> %2$s",request.method(),request.url()));
34+
Timber.i("%1$s -> %2$s",request.method(),request.url());
3335

3436
if(request.headers() != null && request.headers().size() > 0){
3537
Timber.d("Headers:" + request.headers());
3638
}
3739

38-
if(request.body() != null){
39-
RequestBody requestBody = request.body();
40-
final Buffer buffer = new Buffer();
41-
requestBody.writeTo(buffer);
42-
if(isPlaintext(buffer)){
43-
Timber.d("RequestBody:" + buffer.readString(getCharset(requestBody,UTF_8)));
40+
RequestBody requestBody = request.body();
41+
if(requestBody != null){
42+
MediaType mediaType = requestBody.contentType();
43+
Timber.d("RequestContentType:" + mediaType);
44+
if(mediaType != null && multipartType.equalsIgnoreCase(mediaType.type())){
45+
Timber.d("RequestBody:(form data " + requestBody.contentLength() + "-byte body omitted)");
4446
}else{
45-
Timber.d("RequestContentType:" + requestBody.contentType());
46-
Timber.d("RequestBody:(binary " + requestBody.contentLength() + "-byte body omitted)");
47+
final Buffer buffer = new Buffer();
48+
requestBody.writeTo(buffer);
49+
if(isPlaintext(buffer)){
50+
Timber.d("RequestBody:" + buffer.readString(getCharset(requestBody,UTF_8)));
51+
}else{
52+
Timber.d("RequestBody:(binary " + requestBody.contentLength() + "-byte body omitted)");
53+
}
4754
}
4855
}
4956

@@ -59,6 +66,8 @@ public Response intercept(Chain chain) throws IOException {
5966
Response response = chain.proceed(chain.request());
6067
ResponseBody responseBody = response.body();
6168
if(responseBody != null){
69+
MediaType mediaType = responseBody.contentType();
70+
Timber.d("ResponseContentType:" + mediaType);
6271
BufferedSource source = responseBody.source();
6372
source.request(Long.MAX_VALUE); // Buffer the entire body.
6473
Buffer buffer = source.getBuffer();
@@ -68,11 +77,15 @@ public Response intercept(Chain chain) throws IOException {
6877
buffer.writeAll(gzippedResponseBody);
6978
}
7079
}
71-
if(isPlaintext(buffer)){
72-
Timber.d("ResponseBody:" + buffer.clone().readString(getCharset(responseBody,UTF_8)));
80+
81+
if(mediaType != null && multipartType.equalsIgnoreCase(mediaType.type())){
82+
Timber.d("ResponseBody:(form data " + buffer.size() + "-byte body omitted)");
7383
}else{
74-
Timber.d("ResponseContentType:" + responseBody.contentType());
75-
Timber.d("ResponseBody:(binary " + buffer.size() + "-byte body omitted)");
84+
if(isPlaintext(buffer)){
85+
Timber.d("ResponseBody:" + buffer.clone().readString(getCharset(responseBody,UTF_8)));
86+
}else{
87+
Timber.d("ResponseBody:(binary " + buffer.size() + "-byte body omitted)");
88+
}
7689
}
7790
}
7891
return response;

0 commit comments

Comments
 (0)