gpt4 book ai didi

android - HttpLoggingInterceptor (OkHttp3) 为每个请求记录很多次

转载 作者:行者123 更新时间:2023-11-29 01:15:23 26 4
gpt4 key购买 nike

我正在尝试使用 Retrofit2 和 OkHttp3 执行一些请求,并使用 HttpLoggingInterceptor 拦截它们。我正在使用 Dagger 注入(inject) OkHttp 客户端。一切正常,但是当我向我的服务器发出请求时,我可以看到请求和响应记录在 Logcat 中 3 次或更多次,而它应该只是一次。

我设置拦截器的代码部分如下:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(logging);

示例 Logcat 结果,仅针对一个名为的请求:

D/OkHttp: --> GET http://date.jsontest.com/ http/1.1
D/OkHttp: Accept: application/json
D/OkHttp: --> END GET
D/OkHttp: --> GET http://date.jsontest.com/ http/1.1
D/OkHttp: Accept: application/json
D/OkHttp: --> END GET
D/OkHttp: --> GET http://date.jsontest.com/ http/1.1

可能是哪个问题?有什么帮助吗?谢谢

最佳答案

在这里找到答案(2017 年 9 月 24 日评论的 shahsurajk 的答案): https://github.com/square/retrofit/issues/12427

I had an HTTPLogger and chain.proceed(..) in one interceptor itself. So assuming that HTTPLogger internally also calls chain.proceed() which thus makes two requests, I added a networkinterceptor and moved the HTTPLogger to the network interceptor and my custom apikey chain.proceed(...) In other interceptor. Which worked as a charm and the API was called only once internally. Thus you need to look for code which can possibly call the chain.proceed(..) method internally. Which makes a new request.

对我来说,改变了:

httpClient.addInterceptor(logging);

到:

httpClient.addNetworkInterceptor(logging);

关于android - HttpLoggingInterceptor (OkHttp3) 为每个请求记录很多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40088590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com