Spring Rest Template usage causes EOFException Spring Rest Template usage causes EOFException android android

Spring Rest Template usage causes EOFException


This one bit me as well, running Jelly Bean 4.2. After researching, it seems that it's happening because of a combination of Keep-Alive being set and using the standard J2SE HTTP Client, which I believe is HttpURLConnection.

There are 2 solutions that I can confirm are correct.

1) Switch off Keep-Alive.
For me, the solution given in Sebastian's answer, System.setProperty("http.keepAlive", "false"); didn't work. I had to use

HttpHeaders headers = new HttpHeaders();headers.set("Connection", "Close");

and send those headers in an HttpEntity in the RestTemplate.
As mentioned, this solution could have an impact on performance

2) Change the HTTP Client.
In Spring for Android (tested on 1.0.1.RELEASE, but could be in earlier releases too) the default HTTP Client for a RestTemplate instance is determined by the version of Android on the device. API 9 or newer uses HttpURLConnection, older uses HTTPClient. To explicitly set the client to the old one, use

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

More info can be found here: http://static.springsource.org/spring-android/docs/1.0.1.RELEASE/reference/htmlsingle/#d4e34
I'm not sure what impact this will have on performance, but I guess it's more performant than an app that doesn't work.

Anyway, hope that helps someone. I just wasted a week wild-goose-chasing this one down.


http://code.google.com/p/google-http-java-client/issues/detail?id=116 contains a workaround in the latest comment:

This is defenetly somehow connected with keepAlive connections.

When I use: System.setProperty("http.keepAlive", "false"); problems disappears.

But from my understanding keep alive connections are greatly increase performance so it is better not to disable them.

Im also awere that keep alive should be disabled for old versions, but my device is Jelly Bean.

Once applied the error disappeared.
Seems it's not entirely related to Spring, but a JB problem.


Recently I faced this issue and will able to resolved this issue after setting headers with following piece of code :

headers.set("Accept-Language", "en-US,en;q=0.8");