Search code examples
androidandroid-annotations

I/O error for @Post request Android Annotations


I am doing a Post request to the API for my app, when I do this on a Nexus 5 everything goes fine but when I do this request on a HTC One X or an Samsung Galaxy S3 I get the following error:

07-01 11:48:36.605  30101-30251/eu.package.name E/AndroidRuntime﹕ FATAL EXCEPTION: pool-4-thread-3
    org.springframework.web.client.ResourceAccessException: I/O error: null; nested exception is java.io.EOFException
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
            at eu.package.name.Rest.RESTClient_.signinFacebook(RESTClient_.java:155)
            at eu.package.name.Models.User.userLoginFacebook(User.java:91)
            at eu.package.name.Models.User_.access$101(User_.java:14)
            at eu.package.name.Models.User_$2.execute(User_.java:65)
            at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.io.EOFException
            at libcore.io.Streams.readAsciiLine(Streams.java:203)
            at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
            at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
            at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
            at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
            at com.splunk.mint.network.http.MonitorableHttpURLConnection.getResponseCode(MonitorableHttpURLConnection.java:362)
            at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:54)
            at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:80)
            at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
            at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:477)
            at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
            at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
            at eu.package.name.Rest.RESTClient_.signinFacebook(RESTClient_.java:155)
            at eu.package.name.Models.User.userLoginFacebook(User.java:91)
            at eu.package.name.Models.User_.access$101(User_.java:14)
            at eu.package.name.Models.User_$2.execute(User_.java:65)
            at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)

The request I am doing is:

@Post("/signin/facebook")
LoginResponse signinFacebook(FacebookLoginRequest facebookLoginRequest);

I already tried to disable the keep-alive by doing the following:

restClient.setHeader("Connection", "Close");
LoginResponse loginResponse = restClient.signinFacebook(facebookLoginRequest);

Solution

  • I suggest to try out the okhttp client:

    @Rest(rootUrl = "url", requestFactory = OkHttpClientHttpRequestFactory.class)
    

    This should solve lots of problems related to the standard Android http client, including this one. Please note the OkHttpClientHttpRequestFactory is only available in Spring Android 2.0.

    EDIT: You have to declare the following dependencies to use OkHttpClientHttpRequestFactory:

    compile "org.springframework.android:spring-android-rest-template:2.0.0.M2"
    compile "com.squareup.okhttp:okhttp:2.4.0"
    compile "com.squareup.okhttp:okhttp-urlconnection:2.4.0"