没有得到改造日志

Not getting retrofit log

我想查看 Retrofit 请求正文的日志 我实现了下面的代码,用下面的代码我无法获取日志

 public static Retrofit getClient(String baseUrl) {
        Level logLevel = Level.BODY;
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(logLevel);
        new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

遇到这种情况请帮帮我,即使我允许登录开发者选项,仍然无法正常工作

可能是一个错误。您没有将 OkHttpClient 设置为 Retrofit.Builder 这就是它使用默认客户端的原因。更改您的代码如下:-

public static Retrofit getClient(String baseUrl) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.level(HttpLoggingInterceptor.Level.BODY);
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .client(new OkHttpClient.Builder()
                        .addInterceptor(interceptor)
                        .build())
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
  // only instantiate it but not use.
  new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .build();

您可以按照以下方式操作:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new HttpLoggingInterceptor());
Retrofit retrofit = new Retrofit.Builder()  
                  .....
                  .client(httpClient.build())
                  .build();