Http 请求拦截不显示任何日志信息

Http request intercept not showing any log info

我正在尝试拦截一个 http 请求以查看实际发送和接收的数据是什么,但是我的日志中没有显示任何内容。

@WorkerThread
suspend fun webData() {
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY

        val retrofit = Retrofit.Builder().baseUrl(WEB_SERVICE_URL).addConverterFactory(MoshiConverterFactory.create()).build()
        val service = retrofit.create(GetService::class.java)
        val serviceData = service.getData().body()
        getData.postValue(serviceData)
    }

}

你所做的是正确的但不完整,试试这个:

@WorkerThread
suspend fun webData() {
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val client = OkHttpClient.Builder().addInterceptor(interceptor).build()

        val retrofit = Retrofit.Builder().baseUrl(WEB_SERVICE_URL).client(client).addConverterFactory(MoshiConverterFactory.create()).build()
        val service = retrofit.create(GetService::class.java)
        val serviceData = service.getData().body()
        getData.postValue(serviceData)
    }