无法在HttpLoggingInterceptor Retrofit2.0上解析setLevel

时间:2016-09-30 05:25:02

标签: android retrofit2 okhttp3

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

我正在使用它 compile' com.squareup.okhttp3:logging-interceptor:3.3.1' 和Retrofit   compile' com.squareup.retrofit2:converter-gson:2.1.0' 依赖项。

This is where i got error

6 个答案:

答案 0 :(得分:11)

interceptor代码写在getClient()方法中,如

public class RestClient {

    public getClient() {

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

答案 1 :(得分: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结果。

答案 2 :(得分: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

答案 3 :(得分:0)

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

更改

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

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

答案 4 :(得分:0)

https://stackoverflow.com/users/5316836/pitty的好答案 只是想提供更多代码来说明如何将答案与Retrofit.Builder链接

ng update @angular/material

答案 5 :(得分:-1)

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

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