程序在使用OkHttp发出请求时卡在socketRead0方法中。

huangapple go评论74阅读模式
英文:

Program stuck in socketRead0 method when requesting with OkHttp

问题

我开发了一个用于Twitter API的Java库,使用了OkHttp3 4.8.1版本。不幸的是,看起来在发送请求之后,一旦所有操作都完成,程序就不会停止,并且卡在了SocketInputStream中。
程序在使用OkHttp发出请求时卡在socketRead0方法中。

如果不使用缓存,程序会卡在Reference类的waitForReferencePendingList方法中:
程序在使用OkHttp发出请求时卡在socketRead0方法中。

我尝试了各种方法,比如在代码中显式关闭连接,类似于这样的操作,更新了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.
程序在使用OkHttp发出请求时卡在socketRead0方法中。

When not using cache, it is stuck in waitForReferencePendingList method of Reference class instead :
程序在使用OkHttp发出请求时卡在socketRead0方法中。

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 !

huangapple
  • 本文由 发表于 2020年9月8日 03:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63783108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定