简体   繁体   English

HttpLoggingInterceptor不记录

[英]HttpLoggingInterceptor Not Logging

I am running across weird behaviors with HttpLoggingInterceptor. 我遇到了HttpLoggingInterceptor奇怪的行为。 I have noticed that if I use newBuilder() the logging does not work. 我注意到,如果我使用newBuilder(),则日志记录将无法正常工作。

// instantiate object (in app)
val okHttpRequestManager: HttpRequestManager = OkHttpRequestManager(OkHttpClient(), null)

// execute request (in app)
okHttpRequestManager.execute(request, callback)


// another class (in request module)
open class OkHttpRequestManager(private val client: OkHttpClient,
                                 private val httpLoggingInterceptor: HttpLoggingInterceptor?) : HttpRequestExecutor {


    override fun execute(httpRequest: HttpRequest, callback: HttpResponseCallback?) {

        if (httpLoggingInterceptor != null) {
            client.newBuilder().addInterceptor(httpLoggingInterceptor).build()
        }

        // perform request below
        ...
    }
}

The above code snippet does not work. 上面的代码段不起作用。 However, if I make my parameter a builder, everything works fine. 但是,如果我将参数设为构建器,则一切正常。 Is using newBuilder() the incorrect way to do this? 使用newBuilder()是执行此操作的错误方法吗?

// the below works
// another class (in request module)
open class OkHttpRequestManager(private val client: OkHttpClient.Builder,
                                 private val httpLoggingInterceptor: HttpLoggingInterceptor?) : HttpRequestExecutor {


    override fun execute(httpRequest: HttpRequest, callback: HttpResponseCallback?) {

        if (httpLoggingInterceptor != null) {
            // no newBuilder() or build() and works like a charm
            client.addInterceptor(httpLoggingInterceptor) 
        }

        // perform request below
        ...
    }
}

Anyone have an idea as to why this is? 任何人都知道为什么会这样吗?

That's because the method newBuilder() as the name implies, returns the new builder object and when you call build() on it, new instance of OkHttpClient will be returned created from the new builder. 这是因为名称中暗示的方法newBuilder()返回了新的构建器对象,并且当您对其调用build()时,将返回从新的构建器创建的OkHttpClient新实例。

Here is the source code: 这是源代码:

/** Prepares the [request] to be executed at some point in the future. */
  override fun newCall(request: Request): Call {
    return RealCall.newRealCall(this, request, forWebSocket = false)
  }

build() method build()方法

fun build(): OkHttpClient = OkHttpClient(this)

The newBuilder adds to the attributes of the existing client so you will have a new client with both the old and new attributes. newBuilder将添加到现有客户端的属性中,因此您将拥有一个具有旧属性和新属性的新客户端。

If you want to use newBuilder() method then you need to make use of the newly created OkHttpClient . 如果要使用newBuilder()方法,则需要使用新创建的OkHttpClient

// another class (in request module)
open class OkHttpRequestManager(private val client: OkHttpClient,
                                 private val httpLoggingInterceptor: HttpLoggingInterceptor?) : HttpRequestExecutor {


    override fun execute(httpRequest: HttpRequest, callback: HttpResponseCallback?) {

        if (httpLoggingInterceptor != null) {
            val newClient = client.newBuilder().addInterceptor(httpLoggingInterceptor).build()
        }

        // perform request below using newClient
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM