无法在HttpLoggingInterceptor Retrofit2.0上解析setLevel

问题描述 投票:3回答:5

我正在使用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

android retrofit2 okhttp3
5个回答
11
投票

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

public class RestClient {

    public getClient() {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
 }
}

6
投票

将此依赖项添加到应用程序的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结果。


1
投票
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

0
投票

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

更改

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

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

-1
投票

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

        HttpLoggingInterceptor logg = new HttpLoggingInterceptor();
        logg.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logg);
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.