英文:
Program stuck in socketRead0 method when requesting with OkHttp
问题
我开发了一个用于Twitter API的Java库,使用了OkHttp3 4.8.1版本。不幸的是,看起来在发送请求之后,一旦所有操作都完成,程序就不会停止,并且卡在了SocketInputStream
中。
如果不使用缓存,程序会卡在Reference
类的waitForReferencePendingList
方法中:
我尝试了各种方法,比如在代码中显式关闭连接,类似于这样的操作,更新了OkHttp的版本,但问题仍然存在。有什么建议吗?
如果需要的话,这里 是完整的代码,其中执行请求的部分摘要如下:
Request request = new Request.Builder()
.url(url)
.get()
.headers(Headers.of("Authorization", "Bearer " + bearerToken))
.build();
OkHttpClient client = new OkHttpClient.Builder().build()
Response response = client.newCall(request).execute();
String stringResponse = response.body().string();
return Optional.ofNullable(TwitterClient.OBJECT_MAPPER.readValue(stringResponse, classType));
英文:
I developed a Java library for Twitter API here using OkHttp3 4.8.1.
Unfortunately, it looks like after having sent a request, once everything is finished, the program never stops and is stuck in SocketInputStream
.
When not using cache, it is stuck in waitForReferencePendingList
method of Reference
class instead :
I tried everything, closing connection explicitly in my code like this, updating the version of OkHttp, but still the same. Any idea ?
If needed, here is the full code where the request is done, in summary :
Request request = new Request.Builder()
.url(url)
.get()
.headers(Headers.of("Authorization", "Bearer " + bearerToken))
.build();
OkHttpClient client = new OkHttpClient.Builder().build()
Response response = client.newCall(request).execute();
String stringResponse = response.body().string();
return Optional.ofNullable(TwitterClient.OBJECT_MAPPER.readValue(stringResponse, classType));
答案1
得分: 2
最后在其他地方添加了 client.connectionPool().evictAll();
(在我的POST请求中获取Bearer令牌),问题得到解决!
英文:
Finally adding client.connectionPool().evictAll();
elsewhere (in my post request to get a bearer token) solved the problem !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论