我正在尝试获取请求中发送的确切的JSON。下面是我的代码:
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor(){
@Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e(String.format("\nrequest:\n%s\nheaders:\n%s",
request.body().toString(), request.headers()));
com.squareup.okhttp.Response response = chain.proceed(request);
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client).build();
但我只在日志中看到这一点:
request:
com.squareup.okhttp.RequestBody$1@3ff4074d
headers:
Content-Type: application/vnd.ll.event.list+json
在改版2中,您应该使用HttpLoggingInterceptor。
向build.gradle
添加依赖项。截至2019年10月的最新版本为:
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
创建一个reverfit
对象,如下所示:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://backend.example.com")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ApiClient.class);
如果出现弃用警告,只需将setlevel
更改为:
interceptor.level(HttpLoggingInterceptor.Level.BODY);
上面的解决方案为您提供的logcat消息非常类似于
setLogLevel(RestAdapter.LogLevel.FULL)
对于java.lang.ClassNotFoundException
:
较旧的改装版本可能需要较旧的logging-interceptor
版本。请查看评论部分了解详细信息。
我试图使用refrofit2、kotlin和loging-拦截器记录所有请求(使用网络拦截器): 改造:“2.0.2” okhttp3 : “3.2.0” com.squareup.okhttp3:logging-interceptor 3.2.0 喜欢: 它只是打印: 到底发生了什么? -编辑- 记录器不会显示在主线程上执行请求的错误,因此请小心。
null
我正在尝试使用PostSharp和multicastAttribute实现日志记录。使用PostSharp诊断库中的日志功能。 我有一个GlboalAspects。启动项目目录中的cs。内容如下。 右键单击我的项目并选择属性,默认命名空间是“alp_generator” 我不知道“PostSharp.Patterns.Diagnostics.LogAttribute”是否正确。 我根本没有日志记录
问题内容: 我正在使用的Web应用程序偶尔会为某些用户带来数据完整性问题。我想打开跟踪级别的日志记录,但是由于我们每秒要处理100个请求,因此每个日志记录都是不可能的。 log4j是否可以有条件地记录日志?换句话说,我希望仅在特定用户发出请求时才能获得跟踪日志。由于我事先不知道哪些用户会受到影响,因此我无法简单地临时对用户名进行硬编码。 编辑: 我想我需要更清楚一点。我可以轻松地在日志语句中添加条
问题内容: 我需要与执行日志记录使用。 我在没有基本示例的情况下实现了日志记录? 也有小的样本在不使用日志的情况下使用(只是制作了)? 我不知道如何整合它? 问题答案: Spring使我们使用AOP变得非常容易。这是一个简单的日志记录示例: 然后只需配置你的applicationContext.xml(或等效文件): 你会在MyLogger类中注意到我@After在方法上方指定的内容。这被称为建议
问题内容: 我正在迁移一个曾经使用log4j进行logback / slf4j的应用程序,并且已经出现问题约2周了……我做了一个小项目来测试logback / slf4j,它的工作效果很好,但是由于某种原因,在应用它时到实际的应用程序,它只是不想工作。 我正在使用JBoss 6.2,将EAR部署到standalone / deployments目录并启动服务器(除了我得到这个异常的事实,这一切都很