【问题标题】:Error using Retrofit with OkHttp for calling REST service使用带有 OkHttp 的 Retrofit 调用 REST 服务时出错
【发布时间】:2014-11-27 14:25:55
【问题描述】:

我正在使用Retrofit 调用 REST Web API(一个 GET 请求)。

RestAdapter 是:

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint(strEndpoint)
    .build();

这很好用。

不过,有时 http GET 有点慢,所以我在failure(RetrofitError err) 中实现的回调会返回错误,例如:

10-02 16:47:15.279: I/ERROR(16413): error downloading data: failed to connect to myserver.privete.com (port 80) after 15000ms

但是,只有当我使用 wifi 连接时才会出现此错误。

所以我决定尝试更改连接超时,添加自定义 OkClient,使用库 OkHttp

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(60, TimeUnit.SECONDS);
client.setReadTimeout(60, TimeUnit.SECONDS);

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint(Parameters.production ? Parameters.productionEndPoint : Parameters.testEndPoint)
    .setClient(new OkClient(client))
    .build();

但这会立即产生这个错误:

10-02 16:42:38.489: I/ERROR(14259): error downloading data: com.squareup.okhttp.OkHttpClient.open

我使用的版本如下:

Retrofit: 1.6.1 
okhttp: 2.0.0 
okhttp-urlconnection: 1.6.0

编辑:

我也尝试使用 okhttp-urlconnection 2.0.0

【问题讨论】:

  • 您确定 myserver.private.com 与移动设备在同一个网络上吗?

标签: android web-services http-get retrofit okhttp


【解决方案1】:

你可能需要 okhttp-urlconnection 2.0.0。

与此无关,但对于任何未来的问题,您可以使用

获得更好的调试输出
    .setLogLevel(RestAdapter.LogLevel.FULL)

【讨论】:

  • 我也试过使用2.0.0版本的okhttp-urlconnection。
猜你喜欢
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 2021-03-25
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多