每次定位OkHttp问题还是比较麻烦的,所以需要添加拦截器提供更多的日志打印,从而方便定位问题。

原理:我们通过给我们App的OkHttp请求设置拦截器,拦截发出去的请求和返回的请求数据,将结果打印在log里面,方便开发过程中调试,并将结果保存在一个bean对象中,保存到文件当中(我为了方便保存在了sp中),然后写一个列表页面将自己保存的网络请求数据list显示到页面中。

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
public class HttpLogInterceptor implements Interceptor {
private static final Charset UTF8 = Charset.forName("UTF-8");

public enum Level {
/**
* No logs.
*/
NONE,
/**
* Logs request and response lines.
*
* Example:
* {@code
* --> POST /greeting http/1.1 (3-byte body)
*
* <-- 200 OK (22ms, 6-byte body)
* }
*/
BASIC,
/**
* Logs request and response lines and their respective headers.
*
* Example:
* {@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
* <-- END HTTP
* }
*/
HEADERS,
/**
* Logs request and response lines and their respective headers and bodies (if present).
*
* Example:
* {@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
*
* Hi?
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
*
* Hello!
* <-- END HTTP
* }
*/
BODY
}

public interface Logger {
void log(String message);

void log(String tag, String message);

void json(String tag, String message);

/**
* A {@link HttpLogInterceptor.Logger} defaults output appropriate for the current platform.
*/
HttpLogInterceptor.Logger DEFAULT = new HttpLogInterceptor.Logger() {
@Override
public void log(String message) {
com.gjhealth.library.utils.log.Logger.t("http").d(message);
}

@Override
public void log(String tag, String message) {
com.gjhealth.library.utils.log.Logger.t("http" + tag).d(message);
}

@Override
public void json(String tag, String message) {
com.gjhealth.library.utils.log.Logger.t(tag).json(message);
}
};
}

public HttpLogInterceptor() {
this(HttpLogInterceptor.Logger.DEFAULT);
}

public HttpLogInterceptor(HttpLogInterceptor.Logger logger) {
this.logger = logger;
}

private final HttpLogInterceptor.Logger logger;

private volatile HttpLogInterceptor.Level level = HttpLogInterceptor.Level.NONE;

/**
* Change the level at which this interceptor logs.
*/
public HttpLogInterceptor setLevel(HttpLogInterceptor.Level level) {
if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
this.level = level;
return this;
}

public HttpLogInterceptor.Level getLevel() {
return level;
}

@Override
public Response intercept(Chain chain) throws IOException {
HttpLogInterceptor.Level level = this.level;
long startNs = System.nanoTime();

Request request = chain.request();
HttpCacheBean bean = new HttpCacheBean();
bean.requestTime = DateFormatUtils.getCurrentDateTime();
if (level == HttpLogInterceptor.Level.NONE) {
return chain.proceed(request);
}

boolean logBody = level == HttpLogInterceptor.Level.BODY;
boolean logHeaders = logBody || level == HttpLogInterceptor.Level.HEADERS;

RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;

Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
bean.method = request.method();
bean.url = request.url().toString();
bean.protocol = protocol + "";
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
bean.reqContentLength = requestStartMessage;
}
logger.log(requestStartMessage);
bean.requestMsg = requestStartMessage;
if (logHeaders) {
// if (hasRequestBody) {
// // Request body headers are only present when installed as a network interceptor. Force
// // them to be included (when available) so there values are known.
// if (requestBody.contentType() != null) {
// logger.log("Content-Type: " + requestBody.contentType());
// }
// if (requestBody.contentLength() != -1) {
// logger.log("Content-Length: " + requestBody.contentLength());
// }
// }

Headers headers = request.headers();
// StringBuffer sbReq = new StringBuffer();
if (headers.size() > 0) {
JsonObject jsonHeaders = new JsonObject();
for (int i = 0, count = headers.size(); i < count; i++) {
String value = headers.value(i);
if (value != null) {
value = URLDecoder.decode(value, "UTF-8");
}
jsonHeaders.addProperty(headers.name(i), value);
// sbReq.append(headers.name(i) + ":" + value + "\n");
}
logger.json("httphead-req", jsonHeaders.toString());
bean.reqHeaders = jsonHeaders.toString();
}

if (!logBody || !hasRequestBody) {
// logger.log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
// logger.log("--> END " + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);

Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
if (isPlaintext(buffer)) {
logger.json("httprequest", buffer.clone().readString(charset));
bean.requestBody = buffer.readString(charset);
} else {
logger.log("--> END " + request.method() + " (binary "
+ requestBody.contentLength() + "-byte body omitted)");
}
bean.reqContentType = contentType.toString();
}
}
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
logger.log("<-- HTTP FAILED: " + e);
bean.error = e.toString();
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
logger.log("<-- " + response.code() + ' ' + response.message() + ' '
+ response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", "
+ bodySize + " body" : "") + ')');
bean.responseMsg = "<-- " + response.code() + ' ' + response.message() + ' '
+ response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", "
+ bodySize + " body" : "") + ')';
bean.resContentLength = bodySize;
bean.code = response.code();
bean.msg = response.message();
bean.time = tookMs + "ms";

if (logHeaders) {
// StringBuffer sbRes = new StringBuffer();
Headers headers = response.headers();
JsonObject jsonHeaders = new JsonObject();
for (int i = 0, count = headers.size(); i < count; i++) {
String value = headers.value(i);
if (value != null) {
value = URLDecoder.decode(value, "UTF-8");
}
jsonHeaders.addProperty(headers.name(i), value);
// sbRes.append(headers.name(i) + ":" + value + "\n");
// logger.log(headers.name(i) + ": " + headers.value(i));

}
logger.json("httphead-rsp", jsonHeaders.toString());
bean.resHeaders = jsonHeaders.toString();

if (!logBody || !HttpHeaders.hasBody(response)) {

} else if (bodyEncoded(response.headers())) {
logger.log("<-- END HTTP (encoded body omitted)");
} else if (isPlaintext(responseBody.contentType())) {
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();

Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
logger.log("");
logger.log("Couldn't decode the response body; charset is likely malformed.");
logger.log("<-- END HTTP");

return response;
}
}
bean.resContentType = contentType.toString();

// if (!isPlaintext(buffer)) {
// logger.log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
// return response;
// }

if (contentLength != 0) {
// logger.log("");
logger.log("body", buffer.clone().readString(charset));
bean.responseBody = buffer.clone().readString(charset);
}

logger.log("<-- END HTTP (" + buffer.size() + "-byte body)");
}
}

HttpLogManager.saveLogBean(bean);
return response;
}

private static boolean isPlaintext(MediaType mediaType) {
if (mediaType == null) return false;
if (mediaType.type() != null && mediaType.type().equals("text")) {
return true;
}
String subtype = mediaType.subtype();
if (subtype != null) {
subtype = subtype.toLowerCase();
if (subtype.contains("x-www-form-urlencoded") || subtype.contains("json") || subtype.contains("xml") || subtype.contains("html")) //
return true;
}
return false;
}

/**
* Returns true if the body in question probably contains human readable text. Uses a small sample
* of code points to detect unicode control characters commonly used in binary file signatures.
*/
static boolean isPlaintext(Buffer buffer) {
try {
Buffer prefix = new Buffer();
long byteCount = buffer.size() < 64 ? buffer.size() : 64;
buffer.copyTo(prefix, 0, byteCount);
for (int i = 0; i < 16; i++) {
if (prefix.exhausted()) {
break;
}
int codePoint = prefix.readUtf8CodePoint();
if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
return false;
}
}
return true;
} catch (EOFException e) {
return false; // Truncated UTF-8 sequence.
}
}

private boolean bodyEncoded(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
}
}