我正在尝试获取请求中发送的确切JSON。这是我的密码:
OkHttpClient=new OkHttpClient();
client.interceptors().add(新的Interceptor()){
@覆盖公共com.squareup.okhttp.Response intercept(Chain-Chain)引发IOException{
Request=chain.Request();
Log.e(String.format(“\n请求:\n%s\n标题:\n%s”,
request.body().toString(),request.headers());
com.squareup.okhttp.Response=chain.procedue(请求);
返回响应;
}
});
改装改装=新改装.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client.build();
但我只在日志中看到这一点:
请求:
[email protected]
标题:
内容类型:application/vnd.ll.event.list+json
如果删除了我们在改型1中使用的setLog()和setLogLevel(),我该如何正确记录日志
在改装2中,您应该使用HttpLoggingInterceptor
将依赖项添加到build.gradle。截至2019年10月的最新版本为:
实现'com.squareup.okhttp3:日志侦听器:4.2.1'
创建一个改装对象,如下所示:
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
改装改装=新改装.Builder()
.baseUrl(“https://backend.example.com")
.客户(客户)
.addConverterFactory(GsonConverterFactory.create())
.build();
返回改装.create(ApiClient.class);
如果出现弃用警告,只需将setLevel更改为:
拦截器.level(HttpLoggingInterceptor.level.BODY);
上述解决方案为您提供的logcat消息与
setLogLevel(RestAdapter.LogLevel.FULL)
如果java.lang.ClassNotFoundException:
较旧的改装版本可能需要较旧的日志侦听器版本。有关详细信息,请查看评论部分