I’ve got an issue trying to get the Android application (well, Service, it case it makes any difference) to use persistent HTTP 1.1 connections.
The following loop (simplified test case) works through a single TCP session on a desktop JRE, but on an Android device results in the whole socket creation/teardown cycle.
while (true) {
URL url;
try {
url = new URL("http://10.0.0.125:8080/SRV?");
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
The Oracle’s JDK describes something called ‘system properties’:
http.keepAlive=
default: truehttp.maxConnections=
default: 5
Is there something similar in Android’s runtime that prevents persistent connections from being maintained?
Android’s JVM uses the Apache HTTP Components library under the hood for HTTP connections (even those that are done using the java.net interface): as such the behavior is subtly different from the Oracle JVM.
In theory the underlying Harmony code respects the
http.keepAlivesystem property, but whether Google’s copy preserves that behavior isn’t certain to me.If you want to be absolutely certain what’s happening you have to use the HttpComponents code. It is long and painful, but if you look at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html it outlines the connection management approach for http components. Look at section 2.11 which details how to explicitly control the connection management using HTTP components.
Good luck.