Retrofit+OkHttp is ok when sending GET requests bu

2019-06-27 19:17发布

问题:

I'm starting with Retrofit, and I can perform a GET request successfully, but when I try to perform a POST or PUT request, I get a SocketTimeOut Exception.

I added OkHttp to my libs folder according this question but it didn't solve the problem.

Here is the interface definition:

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 );
}

and this is how I'm performing the POST request:

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();
  }
} );

After the execution of webservice.saveOrder(...) I don't see any requests in the server log (I see logs for every GET request, though). The cause for the RetrofitError is retrofit.RetrofitError: java.net.SocketTimeoutException.

All libraries are updated (Retrofit 1.5.0 and OkHttp 1.5.3), and I'm using a real device running Android 4.4.2.

Unlike the issue reported on GitHub, this error always occurs for POST or PUT requests.

Thanks in advance!

Edit

After enabling Retrofit FULL Log, I noticed that after a PUT request, I can't connect to the server anymore (even for GET requests). In the logs below is possible to see a successful GET request, what follows is an "apparently" successful PUT request (that don't hit the server - no logs in the server at all for this request) and then another PUT request that throws a ConnectException. After that, even the same previous GET requests aren't being sent to the server - and dispatches the same exception. Any thoughts on this?

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

Edit 2

This error occurred in a Android 4.4.2, but not in a device running Android 4.1.0.

回答1:

Check out my comment on the Previously mentioned thread. In your code, it seems that the POST requests don't have the final slash "/"