发送GET请求时,Retrofit + OkHttp正常,但发送POST时提供SocketTimetout

时间:2014-04-04 22:51:53

标签: java android http retrofit

我从Retrofit开始,我可以成功执行GET请求,但是当我尝试执行POST或PUT请求时,我得到一个SocketTimeOut异常。

我根据this question将OkHttp添加到我的libs文件夹中,但它没有解决问题。

这是界面定义:

public interface MyService {
    @GET("/orders.json")
    void listOrders( Callback<List<Order>> callback );

    @GET("/forms.json")
    void listForms( Callback<List<Form>> callback );

    @POST("/orders/{id}.json")
    void saveOrder( @Path("id") int orderId, @Body Order order, Callback<Order> callback );
}

这就是我执行POST请求的方式:

private RestAdapter restAdapter;
private MyService webservice;

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

webservice = restAdapter.create(MyService.class);

webservice.saveOrder( (int) _chamado.getId(), _chamado, new Callback<Order>() {

  @Override
  public void success(Order arg0, Response arg1) {
    Toast.makeText(MyActivity.this, "Request OK", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void failure(RetrofitError arg0) {
    Toast.makeText(MyActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
  }
} );

执行webservice.saveOrder(...)后,我在服务器日志中看不到任何请求(我看到每个GET请求的日志)。 RetrofitError的原因是retrofit.RetrofitError: java.net.SocketTimeoutException

所有库都已更新(Retrofit 1.5.0和OkHttp 1.5.3),我正在使用运行Android 4.4.2的真实设备。

GitHub上报告的问题不同,对于POST或PUT请求,此错误始终

提前致谢!

修改

启用Retrofit FULL Log后,我注意到在PUT请求之后,我无法再连接到服务器(即使是GET请求)。在下面的日志中可以看到一个成功的GET请求,接下来是一个“显然”成功的PUT请求(没有命中服务器 - 根本没有服务器中的日志),然后另一个PUT请求抛出一个ConnectException。之后,即使是相同的先前GET请求也不会被发送到服务器 - 并调度相同的异常。有什么想法吗?

04-11 18:00:41.291: D/Retrofit(16390): ---> HTTP GET http://10.0.0.104:3000/api/v1/orders.json
04-11 18:00:41.291: D/Retrofit(16390): ---> END HTTP (0-byte body)
04-11 18:00:42.008: D/Retrofit(16390): <--- HTTP 200 http://10.0.0.104:3000/api/v1/orders.json (716ms)
04-11 18:00:42.015: D/Retrofit(16390): : HTTP/1.1 200 OK 
04-11 18:00:42.015: D/Retrofit(16390): Cache-Control: max-age=0, private, must-revalidate
04-11 18:00:42.015: D/Retrofit(16390): Connection: Keep-Alive
04-11 18:00:42.015: D/Retrofit(16390): Content-Length: 404
04-11 18:00:42.015: D/Retrofit(16390): Content-Type: application/json; charset=utf-8
04-11 18:00:42.016: D/Retrofit(16390): Date: Fri, 11 Apr 2014 21:00:44 GMT
04-11 18:00:42.017: D/Retrofit(16390): Etag: "4acd7d8cacb097f6f5ffaddcd468dc06"
04-11 18:00:42.018: D/Retrofit(16390): OkHttp-Received-Millis: 1397250041960
04-11 18:00:42.018: D/Retrofit(16390): OkHttp-Response-Source: NETWORK 200
04-11 18:00:42.018: D/Retrofit(16390): OkHttp-Sent-Millis: 1397250041293
04-11 18:00:42.019: D/Retrofit(16390): Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
04-11 18:00:42.020: D/Retrofit(16390): Set-Cookie: request_method=GET; path=/
04-11 18:00:42.021: D/Retrofit(16390): X-Content-Type-Options: nosniff
04-11 18:00:42.022: D/Retrofit(16390): X-Frame-Options: SAMEORIGIN
04-11 18:00:42.023: D/Retrofit(16390): X-Request-Id: be381a81-210a-48d2-aeb0-cabb9b945818
04-11 18:00:42.023: D/Retrofit(16390): X-Runtime: 0.010391
04-11 18:00:42.023: D/Retrofit(16390): X-Ua-Compatible: chrome=1
04-11 18:00:42.024: D/Retrofit(16390): X-Xss-Protection: 1; mode=block
04-11 18:00:42.027: D/Retrofit(16390): [{"id":1,"date":"2014-01-01","place":"Av. Paulista, 2000","order_cod":"934","form_data":"{}","form_cod":"1","form_id":2,"created_at":null,"updated_at":"2014-04-11T13:53:31.000Z"},{"id":6,"date":"2016-07-06","place":"Rua Dom Gerardo, Centro, Rio de Janeiro","order_cod":"023984","form_data":"","form_cod":"1","form_id":null,"created_at":"2014-03-28T13:02:37.000Z","updated_at":"2014-03-28T13:02:37.000Z"}]
04-11 18:00:42.027: D/Retrofit(16390): <--- END HTTP (404-byte body)

04-11 18:00:51.278: D/Retrofit(16390): ---> HTTP PUT http://10.0.0.104:3000/api/v1/orders/1.json
04-11 18:00:51.278: D/Retrofit(16390): Content-Type: application/json; charset=UTF-8
04-11 18:00:51.279: D/Retrofit(16390): Content-Length: 104
04-11 18:00:51.280: D/Retrofit(16390): {"date":"2014-01-01","form_data":":)","place":"Av. Paulista, 2000","order_cod":"934","form_id":1,"id":1}
04-11 18:00:51.281: D/Retrofit(16390): ---> END HTTP (104-byte body)

04-11 18:01:54.371: D/Retrofit(16390): ---> HTTP PUT http://10.0.0.104:3000/api/v1/orders/1.json
04-11 18:01:54.371: D/Retrofit(16390): Content-Type: application/json; charset=UTF-8
04-11 18:01:54.371: D/Retrofit(16390): Content-Length: 104
04-11 18:01:54.372: D/Retrofit(16390): {"date":"2014-01-01","form_data":":)","place":"Av. Paulista, 2000","order_cod":"934","form_id":1,"id":1}
04-11 18:01:54.373: D/Retrofit(16390): ---> END HTTP (104-byte body)

04-11 18:01:57.370: D/Retrofit(16390): ---- ERROR http://10.0.0.104:3000/api/v1/orders/1.json
04-11 18:01:57.389: D/Retrofit(16390): java.net.ConnectException: failed to connect to /10.0.0.104 (port 3000) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:01:57.389: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:223)
04-11 18:01:57.389: D/Retrofit(16390):  at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
04-11 18:01:57.389: D/Retrofit(16390):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-11 18:01:57.389: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-11 18:01:57.389: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
04-11 18:01:57.389: D/Retrofit(16390):  at java.net.Socket.connect(Socket.java:833)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:248)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.Connection.connect(Connection.java:98)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:236)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:180)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:366)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:103)
04-11 18:01:57.389: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:205)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:66)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
04-11 18:01:57.389: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-11 18:01:57.389: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-11 18:01:57.389: D/Retrofit(16390):  at retrofit.Platform$Android$2$1.run(Platform.java:142)
04-11 18:01:57.389: D/Retrofit(16390):  at java.lang.Thread.run(Thread.java:841)
04-11 18:01:57.389: D/Retrofit(16390): Caused by: libcore.io.ErrnoException: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:01:57.389: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:208)
04-11 18:01:57.389: D/Retrofit(16390):  ... 22 more
04-11 18:01:57.390: D/Retrofit(16390): ---- END ERROR

04-11 18:02:03.557: D/Retrofit(16390): ---> HTTP PUT http://10.0.0.104:3000/api/v1/orders/1.json
04-11 18:02:03.557: D/Retrofit(16390): Content-Type: application/json; charset=UTF-8
04-11 18:02:03.557: D/Retrofit(16390): Content-Length: 104
04-11 18:02:03.558: D/Retrofit(16390): {"date":"2014-01-01","form_data":":)","place":"Av. Paulista, 2000","order_cod":"934","form_id":1,"id":1}
04-11 18:02:03.558: D/Retrofit(16390): ---> END HTTP (104-byte body)

04-11 18:02:06.561: D/Retrofit(16390): ---- ERROR http://10.0.0.104:3000/api/v1/orders/1.json
04-11 18:02:06.576: D/Retrofit(16390): java.net.ConnectException: failed to connect to /10.0.0.104 (port 3000) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:02:06.576: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:223)
04-11 18:02:06.576: D/Retrofit(16390):  at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
04-11 18:02:06.576: D/Retrofit(16390):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-11 18:02:06.576: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-11 18:02:06.576: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
04-11 18:02:06.576: D/Retrofit(16390):  at java.net.Socket.connect(Socket.java:833)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:248)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.Connection.connect(Connection.java:98)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:236)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:180)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:366)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:103)
04-11 18:02:06.576: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:205)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:66)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
04-11 18:02:06.576: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-11 18:02:06.576: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-11 18:02:06.576: D/Retrofit(16390):  at retrofit.Platform$Android$2$1.run(Platform.java:142)
04-11 18:02:06.576: D/Retrofit(16390):  at java.lang.Thread.run(Thread.java:841)
04-11 18:02:06.576: D/Retrofit(16390): Caused by: libcore.io.ErrnoException: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:02:06.576: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:208)
04-11 18:02:06.576: D/Retrofit(16390):  ... 22 more
04-11 18:02:06.576: D/Retrofit(16390): ---- END ERROR

04-11 18:02:13.189: D/Retrofit(16390): ---> HTTP GET http://10.0.0.104:3000/api/v1/orders.json
04-11 18:02:13.189: D/Retrofit(16390): ---> END HTTP (0-byte body)

04-11 18:02:16.190: D/Retrofit(16390): ---- ERROR http://10.0.0.104:3000/api/v1/orders.json
04-11 18:02:16.203: D/Retrofit(16390): java.net.ConnectException: failed to connect to /10.0.0.104 (port 3000) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:02:16.203: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:223)
04-11 18:02:16.203: D/Retrofit(16390):  at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
04-11 18:02:16.203: D/Retrofit(16390):  at libcore.io.IoBridge.connect(IoBridge.java:112)
04-11 18:02:16.203: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-11 18:02:16.203: D/Retrofit(16390):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
04-11 18:02:16.203: D/Retrofit(16390):  at java.net.Socket.connect(Socket.java:833)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:248)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.Connection.connect(Connection.java:98)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:236)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:180)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:366)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:319)
04-11 18:02:16.203: D/Retrofit(16390):  at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:484)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:71)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:358)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
04-11 18:02:16.203: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-11 18:02:16.203: D/Retrofit(16390):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-11 18:02:16.203: D/Retrofit(16390):  at retrofit.Platform$Android$2$1.run(Platform.java:142)
04-11 18:02:16.203: D/Retrofit(16390):  at java.lang.Thread.run(Thread.java:841)
04-11 18:02:16.203: D/Retrofit(16390): Caused by: libcore.io.ErrnoException: isConnected failed: EHOSTUNREACH (No route to host)
04-11 18:02:16.203: D/Retrofit(16390):  at libcore.io.IoBridge.isConnected(IoBridge.java:208)
04-11 18:02:16.203: D/Retrofit(16390):  ... 22 more
04-11 18:02:16.203: D/Retrofit(16390): ---- END ERROR

修改2

此错误发生在Android 4.4.2中,但在运行Android 4.1.0的设备中没有。

1 个答案:

答案 0 :(得分:0)

查看我对Previously mentioned thread的评论。在您的代码中,似乎POST请求没有最终的斜杠&#34; /&#34;