setLevel okhttp LoggingInterceptor deprecated

Viewed 4803

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

what should replace with setLevel? to remove the deprecated issue

3 Answers

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

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

in Java:

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

in Kotlin

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

Happy coding :)

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...

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()
Related