无法在HttpLoggingInterceptor Retrofit2.0上解析setLevel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法在HttpLoggingInterceptor Retrofit2.0上解析setLevel相关的知识,希望对你有一定的参考价值。

我正在使用Retrofit,我想记录我的回复和其他事情,我正在使用这个https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses类型但我面临无法解决setLevel错误

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  

logging.setLevel(Level.BODY); // i am getting error on this

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  

httpClient.addInterceptor(logging); //this is also getting error

我正在使用这个编译'com.squareup.okhttp3:logging-interceptor:3.3.1'和Retrofit编译'com.squareup.retrofit2:converter-gson:2.1.0'依赖。

This is where i got error

答案

interceptor方法中写下你的getClient()代码

public class RestClient {

    public getClient() {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
 }
}
另一答案

将此依赖项添加到应用程序的gradle:

 compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

并使用如下:如下所示构建您的OkHttpClient

final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
okHttpClientBuilder.connectTimeout(RestConstants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
return okHttpClientBuilder.build();

并将其设置为您的Retrofit作为客户端。

我建议你检查你的应用程序是否是Debuggable,而不是设置你的日志拦截器。所以在生产中你不会记录api结果。

另一答案
define following plugins of retrofit in build.gradle(Mobile:app) correctly as show below...

dependencies {
 ..........
   implementation('com.squareup.retrofit2:retrofit:2.3.0')
            {
                exclude module: 'okhttp'
            }
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
}

Works For Sure....@Ambilpura Sunil
另一答案

如果您使用的是okhttp3,请确保导入okhttp3(而不是okhttp)

更改

implementation 'com.squareup.okhttp:logging-interceptor:3.14.1'

implementation 'com.squareup.okhttp3:logging-interceptor:3.14.1'
另一答案

你可以这样做,我的问题解决: -

        HttpLoggingInterceptor logg = new HttpLoggingInterceptor();
        logg.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logg);

以上是关于无法在HttpLoggingInterceptor Retrofit2.0上解析setLevel的主要内容,如果未能解决你的问题,请参考以下文章

HttpLoggingInterceptor 干扰刷新令牌机制

Android Retrofit2 0 查看log和JSON字符串(HttpLoggingInterceptor)

Android Retrofit2.0 查看log和JSON字符串(HttpLoggingInterceptor)

Android HttpLoggingInterceptor的用法简介

如何为 KTOR 添加日志拦截器?

http解析异常阿波罗Graphql