setLevel okhttp LoggingInterceptor deprecated

2020-08-10 07:11发布

问题:

setLevel(okhttp3.logging.HttpLoggingInterceptor.Level)' is deprecated

what should replace with setLevel? to remove the deprecated issue

回答1:

According to the documentation "Moved to var. Replace setLevel(...) with level(...) to fix Java",

Replace setLevel(...) with level(...) will fix this issue

example :

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

Happy coding :)



回答2:

for Kotlin

replace :

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

with :

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

Add your inceptor to okHttpClient

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

Happy Coding...



回答3:

For Kotlin, use the apply function and then setLevel using the level property.

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

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