【问题标题】:How to call httpLoggingInterceptor inside request.newBuilder()如何在 request.newBuilder() 中调用 httpLoggingInterceptor
【发布时间】:2021-09-11 14:28:24
【问题描述】:

据我研究发现,.client(OkHttptvariableName)
在构建器内部使用,以便将 api 响应打印到 logcat。

但就我而言,我无法使用 request.newBuilder 调用 .client inbuild 方法。
以下是我的代码 sn-ps。
请帮帮我。

 val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        val okHttpClient: OkHttpClient = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()


when {
            UserInfo.loginStatus -> {
                request = request.newBuilder()
                    .header("Authorization", UserInfo.token)
                    .build()
            }
            else -> {
                request = request.newBuilder()

                    .build()
            }
        }

【问题讨论】:

  • 您的预期行为是什么
  • @NehaRathore ,我想在 Logcat 中打印 api 响应。

标签: android api kotlin retrofit response


【解决方案1】:

看这个例子:

        val httpClient = OkHttpClient.Builder()

        if (BuildConfig.DEBUG) {
            val logging = HttpLoggingInterceptor()
            logging.setLevel(HttpLoggingInterceptor.Level.BODY)
            httpClient.addInterceptor(logging)
        }

        Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build()
    }

更多信息请见:https://futurestud.io/tutorials/retrofit-2-enable-logging-for-development-builds-only

【讨论】:

    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2020-06-30
    • 2017-08-07
    • 2018-10-25
    • 2017-02-26
    • 2020-04-16
    相关资源
    最近更新 更多