【问题标题】:setLevel okhttp LoggingInterceptor deprecated不推荐使用 setLevel okhttp LoggingInterceptor
【发布时间】:2020-09-11 11:40:45
【问题描述】:

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

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

【问题讨论】:

    标签: android retrofit2 okhttp


    【解决方案1】:

    根据文档“Moved to var. Replace setLevel(...) with level(...) to fix Java”,

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

    在 Java 中:

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

    在 Kotlin 中

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

    编码愉快:)

    【讨论】:

    • 在 Kotlin 中相同:val logging = HttpLoggingInterceptor() logging.level = HttpLoggingInterceptor.Level.BODY
    【解决方案2】:

    对于 Kotlin

    替换:

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

    与:

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

    将您的 Inceptor 添加到 okHttpClient

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

    快乐编码...

    【讨论】:

      【解决方案3】:

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

       private val loggingInterceptor = HttpLoggingInterceptor().apply {
              level = HttpLoggingInterceptor.Level.BODY
          }
      
          var client : OkHttpClient = OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()
      

      【讨论】:

      • 请不要只发布代码作为答案,而是解释您的代码的作用以及它如何解决问题。带有解释的答案通常质量更高,更有可能吸引投票。
      猜你喜欢
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2012-11-14
      • 2019-11-08
      • 2019-11-08
      • 2020-01-03
      • 2011-12-02
      相关资源
      最近更新 更多