One of the nice features of spring-android RestTemplate is the use of appropriate (recommended by Google) implementation of RequestFactory depending on the version of OS.
Google recommends to use the J2SE facilities on Gingerbread (Version
2.3) and newer, while previous versions should use the HttpComponents HttpClient. Based on this recommendation RestTemplate checks the
version of Android on which your app is running and uses the
appropriate ClientHttpRequestFactory.
So the previous answer is not full because HttpComponentsClientHttpRequestFactory (which is used by spring-android for Android OS versions < 2.3) is not taken into consideration.
My solution was something like this:
public class MyRestTemplate extends RestTemplate {
public MyRestTemplate() {
if (getRequestFactory() instanceof SimpleClientHttpRequestFactory) {
Log.d("HTTP", "HttpUrlConnection is used");
((SimpleClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(10 * 1000);
((SimpleClientHttpRequestFactory) getRequestFactory()).setReadTimeout(10 * 1000);
} else if (getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
Log.d("HTTP", "HttpClient is used");
((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setReadTimeout(10 * 1000);
((HttpComponentsClientHttpRequestFactory) getRequestFactory()).setConnectTimeout(10 * 1000);
}
}
}