参考:
Retrofit使用Log拦截器在控制台输出Log
解决华为手机无法输出Debug级别log的问题
导入依赖:
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
使用log拦截器:
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//日志级别
builder.addInterceptor(loggingInterceptor);
}
OkHttpClient client = builder.build();
Retrofit mRetrofit = new Retrofit.Builder()
.client(client)
.build();
打印如下:
D/OkHttp: --> GET https://api.douban.com/v2/movie/top250?start=0&count=10 http/1.1
D/OkHttp: --> END GET
D/OkHttp: <-- 200 OK https://api.douban.com/v2/movie/top250?start=0&count=10 (1123ms)
D/OkHttp: Date: Wed, 04 Apr 2018 03:49:23 GMT
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Transfer-Encoding: chunked
D/OkHttp: Connection: keep-alive
D/OkHttp: Keep-Alive: timeout=30
D/OkHttp: Vary: Accept-Encoding
D/OkHttp: X-Ratelimit-Remaining2: 90
D/OkHttp: X-Ratelimit-Limit2: 100
D/OkHttp: Expires: Sun, 1 Jan 2006 01:00:00 GMT
D/OkHttp: Pragma: no-cache
D/OkHttp: Cache-Control: must-revalidate, no-cache, private
D/OkHttp: Set-Cookie: bid=vzLXXRxVlk8; Expires=Thu,
04-Apr-19 03:49:23 GMT; Domain=.douban.com; Path=/
D/OkHttp: X-DOUBAN-NEWBID: vzLXXRxVlk8
D/OkHttp: X-DAE-Node: dis12
D/OkHttp: X-DAE-App: movie
D/OkHttp: Server: dae
D/OkHttp: {"count": 10, "start": 0, "total": 250,
"subjects": [{"rating": {"max": 10, "average":
9.6, "stars": "50", "min": 0}, "genres":............
D/OkHttp: <-- END HTTP (23599-byte body)
使用okhttp过滤。
依赖:
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
定义拦截器:
@NonNull
private static HttpLoggingInterceptor getHttpLoggingInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor =
new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
try {
String text = URLDecoder.decode(message, "utf-8");
Log.e("=======", text);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("======", e.getMessage());
}
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return httpLoggingInterceptor;
}
使用:
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
builder.addInterceptor(getHttpLoggingInterceptor());
}
OkHttpClient client = builder.build();
Retrofit mRetrofit = new Retrofit.Builder()
.client(client)
.build();
log如下:
E/=======: <-- 200 OK https://api.douban.com/v2/movie/top250?start=0&count=10 (1027ms)
E/=======: Date: Wed, 04 Apr 2018 04:38:13 GMT
E/=======: Content-Type: application/json; charset=utf-8
E/=======: Transfer-Encoding: chunked
E/=======: Connection: keep-alive
E/=======: Keep-Alive: timeout=30
E/=======: Vary: Accept-Encoding
E/=======: X-Ratelimit-Remaining2: 99
E/=======: X-Ratelimit-Limit2: 100
E/=======: Expires: Sun, 1 Jan 2006 01:00:00 GMT
E/=======: Pragma: no-cache
E/=======: Cache-Control: must-revalidate, no-cache, private
E/=======: Set-Cookie: bid=e8XDmXKtunk; Expires=Thu,
04-Apr-19 04:38:13 GMT; Domain=.douban.com; Path=/
E/=======: X-DOUBAN-NEWBID: e8XDmXKtunk
E/=======: X-DAE-Node: sindar21c
E/=======: X-DAE-App: movie
E/=======: Server: dae
E/=======: {"count": 10, "start": 0, "total": 250,
"subjects": [{"rating": {"max": 10, "average": 9.6,
"stars": "50", "m.............................
E/=======: <-- END HTTP (23599-byte body)
定义拦截器:
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();//请求发起的时间
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))
.append("=")
.append(body.encodedValue(i))
.append(",");
}
sb.delete(sb.length() - 1, sb.length());
Log.d("---POST---",
String.format("发送请求 %s on %s %n%s %nRequestParams:{%s}",
request.url(),
chain.connection(),
request.headers(),
sb.toString()));
}
} else {
Log.d("---GET---", String.format("发送请求 %s on %s%n%s",
request.url(),
chain.connection(),
request.headers()));
}
Response response = chain.proceed(request);
long t2 = System.nanoTime();//收到响应事件
ResponseBody responseBody = response.peekBody(1024 * 1024);
Log.d("---response---",
String.format("接收响应: [%s] %n返回json:【%s】 %.1fms %n%s",
response.request().url(),
responseBody.string(),
(t2 - t1) / 1e6d,
response.headers()
));
return response;
}
}
使用:
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
builder.addInterceptor(new LoggingInterceptor());
}
OkHttpClient client = builder.build();
Retrofit mRetrofit = new Retrofit.Builder()
.client(client)
.build();
log如下:
D/---GET---: 发送请求 https://api.douban.com/v2/movie/top250?start=0&count=10 on null
D/---response---: 接收响应: [https://api.douban.com/v2/movie/top250?start=0&count=10]
返回json:【{"count": 10, "start": 0, "total": 250,
"subjects": [{"rating": {"max": 10......................