我读了https://futurestud.io/blog/retrofit-2-log-requests-and-responses并成功了。
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(logging);
client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(LoginApiService.Login_API_URL)
.build();
但它返回的结果如
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> POST http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ http/1.1
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json; charset=UTF-8
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Length: 46
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> END POST
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- 400 Bad Request http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ (96ms)
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Date: Wed, 07 Sep 2016 20:11:27 GMT
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Server: WSGIServer/0.2 CPython/3.4.3
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Vary: Accept
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Allow: POST, OPTIONS
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: X-Frame-Options: SAMEORIGIN
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- END HTTP
这还不够。
因为我使用RESTful api和json类型,
我该怎么办?
答案 0 :(得分:1)
如果你看一下HttpLoggingInterceptor.setLevel的代码:
public HttpLoggingInterceptor setLevel(Level level) {
if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
this.level = level;
return this;
}
您会看到每次调用此方法时,您都会覆盖最后一次调用,所以这样做:
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
将等级设为Level.HEADERS
这正是您的日志中发生的事情,您只看到标题
只需删除该第(2)行,您就可以Level.BODY
这就是您正在寻找的内容。