我试图获得在请求中发送的确切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

我应该如何做正确的日志记录,给定的删除setLog()和setLogLevel(),我们用来使用的Retrofit 1?


当前回答

当你和我试图问《Retrofit:爱在Android上使用api》一书的作者时,我遇到了这个问题(这里是链接)。 (不!)我不会为他们做广告....但他们真的是好人:) 作者很快就回复了我,在Retrofit 1.9和Retrofit 2.0 beta上都使用了Log方法。

下面是Retrofit 2.0 beta的代码:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
// set your desired log level
logging.setLevel(Level.BODY);

OkHttpClient httpClient = new OkHttpClient();  
// add your other interceptors …

// add logging as last interceptor
httpClient.interceptors().add(logging);  // <-- this is the important line!

Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient)
   .build();

这是如何在HttpLoggingInterceptor的帮助下添加日志方法。另外,如果你是我上面提到的那本书的读者,你可能会发现它说在Retrofit 2.0中没有日志方法了——我问过作者,这是不正确的,他们明年会更新这本书来讨论这个问题。

//如果你不太熟悉Retrofit中的Log方法,我想分享更多的东西。

还应该注意,您可以选择一些日志级别。我使用关卡。BODY,它会给出这样的东西:

你可以在图片中找到几乎所有的http人员:头,内容和响应,等等。

有时候你真的不需要所有的客人都来参加你的派对:我只想知道它是否成功连接,在我的activity & Fragmetn中是否成功拨打互联网电话。然后你就可以自由使用Level了。BASIC,它将返回类似这样的东西:

你能找到里面的状态码200 OK吗?就是这样:)

还有一个,Level。HEADERS,它只返回网络的头信息。当然,这是另一张图:

这就是所有的日志技巧;)

我想和你们分享我在那里学到了很多的教程。他们有一堆很棒的帖子,谈论几乎所有与改造有关的事情,他们还在继续更新帖子,与此同时,改造2.0即将到来。请看一下这些工作,我想这会节省你很多时间。

其他回答

如果您正在使用Retrofit2和okhttp3,那么您需要知道Interceptor是通过队列工作的。所以在你的其他interceptor之后添加loggingInterceptor:

HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        if (BuildConfig.DEBUG)
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

 new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new CatalogInterceptor(context))//first
                .addInterceptor(new OAuthInterceptor(context))//second
                .authenticator(new BearerTokenAuthenticator(context))
                .addInterceptor(loggingInterceptor)//third, log at the end
                .build();

你也可以添加Facebook的Stetho,在Chrome中查看网络痕迹: http://facebook.github.io/stetho/

final OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
    builder.networkInterceptors().add(new StethoInterceptor());
}

然后打开“chrome://inspect”在chrome…

试试这个:

Request request = chain.request();
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
String body = buffer.readUtf8();

在此之后,在正文中有您感兴趣的JSON。

首先向build.gradle添加依赖项:

实现“com.squareup.okhttp3:日志记录拦截器:3.12.1”

在使用Kotlin时,你可以像这样添加日志拦截器:

companion object {
    val okHttpClient = OkHttpClient().newBuilder()
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
            .build()


    fun getRetrofitInstance(): Retrofit {
        val retrofit = Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(ScanNShopConstants.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build()

        return retrofit
    }
}

这将创建一个带有Logging的改装对象。不需要创建单独的对象。

 private static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(new OkHttpClient().newBuilder()
                    .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                    .readTimeout(READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .connectTimeout(CONNECTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)
                    .build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();