18034727885
2026-03-17 162564950c599a0d97ed9aade4301f8f6d285fbe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.igo.user.network;
 
import android.util.Log;
 
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
 
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
 
public class LoggingInterceptor implements Interceptor {
 
    private static final String TAG = "zmc-Log";
 
    private volatile Level level = Level.NONE;
 
    public Level getLevel() {
        return level;
    }
 
    public void setLevel(Level l) {
        level = l;
    }
 
    public enum Level {
        NONE,
        BODY
    }
 
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
 
        long startTime = System.currentTimeMillis();
        Log.d(TAG + "-" + Thread.currentThread().getId(), "----------Start----------------");
        if ("GET".equalsIgnoreCase(request.method()) || "DELETE".equalsIgnoreCase(request.method())) {
            Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + URLDecoder.decode(request.toString(), StandardCharsets.UTF_8.name()));
        } else {
            String requestInfo = "Request{method=" + request.method() + ", url=" + request.url() + ", head=" + request.headers() + "}";
            Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + requestInfo);
            try {
                String jsonInfo = "RequestParams:" + URLDecoder.decode(bodyToString(request), StandardCharsets.UTF_8.name());
                Log.d(TAG + "-" + Thread.currentThread().getId(), "| " + jsonInfo);
            } catch (Exception e) {
                Log.e(TAG + "-" + Thread.currentThread().getId(), "| " + "RequestParams:" + bodyToString(request));
                Log.e(TAG + "-" + Thread.currentThread().getId(), "| " + "error:" + e.getMessage());
            }
        }
 
        Response response = chain.proceed(request);
        long endTime = System.currentTimeMillis();
 
        String bodyString = response.body().string();
        Log.d(TAG + "-" + Thread.currentThread().getId(), "| Response:" + bodyString);
        long duration = endTime - startTime;
        Log.d(TAG + "-" + Thread.currentThread().getId(), "----------End:" + duration + "毫秒----------");
 
        ResponseBody newBody = ResponseBody.create(response.body().contentType(), bodyString);
        return response.newBuilder()
                .body(newBody)
                .build();
    }
 
    private String bodyToString(Request request) {
        try {
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
 
            if (copy.body() instanceof MultipartBody) {
                MultipartBody multipartBody = (MultipartBody) copy.body();
                for (int i = 0; i < multipartBody.size(); i++) {
                    final MultipartBody.Part part = multipartBody.part(i);
                    MediaType mediaType = part.body().contentType();
                    if (!isFilePart(mediaType)) {
                        part.body().writeTo(buffer);
                    } else {
                        buffer.writeUtf8("file params Content-Type: " + mediaType + "\n");
                    }
                }
            } else {
                copy.body().writeTo(buffer);
            }
            return buffer.readUtf8();
        } catch (IOException e) {
            return "did not work";
        }
    }
 
    // 是否为文件
    boolean isFilePart(MediaType mediaType) {
        if (mediaType != null) {
            String type = mediaType.toString();
            return type.startsWith("audio/") || type.startsWith("text/plain");
        }
        return false;
    }
}