setLevel okhttp LoggingInterceptor已弃用

时间:2019-07-01 18:12:20

标签: android retrofit2 okhttp3

setLevel(okhttp3.logging.HttpLoggingInterceptor.Level)'已弃用

应该用setLevel替换什么?删除已弃用的问题

3 个答案:

答案 0 :(得分:6)

根据文档“移至var。将setLevel(...)替换为level(...)以修复Java”,

setLevel(...)替换为level(...)将解决此问题

示例:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.level(HttpLoggingInterceptor.Level.BODY);

答案 1 :(得分:0)

对于Kotlin

替换:

val logger: HttpLoggingInterceptor =
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)  //Logging Interceptor

与:

val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY

将您的受体添加到okHttpClient

val okkHttpclient = OkHttpClient.Builder()
                .addInterceptor(networkConnectionInterceptor)
                .addInterceptor(logger)
                .build()

快乐编码...

答案 2 :(得分:0)

对于Kotlin,请使用apply函数,然后使用level属性设置setLevel。

 private val loggingInterceptor = HttpLoggingInterceptor().apply {
        level = HttpLoggingInterceptor.Level.BODY
    }

    var client : OkHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()