Java – persistent httpurlconnections on Android

I have a problem trying to get an Android application using a persistent HTTP 1.1 connection (well, service, it's different)

The following loop (simplified test case) works through a single TCP session on the desktop JRE, but results in the entire socket creation / removal cycle on Android devices

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

Oracle's JDK describes something called "system properties":

Solution

Android JVMs use the Apache HTTP components library for HTTP connections (even those that use the java.net interface): therefore, this behavior is slightly different from Oracle JVMs

In theory, the underlying harmony code respects http Keepalive system attribute, but I'm not sure whether Google's copy retains this behavior

If you want to be absolutely sure what happened, you must use httpcomponents code It's long and painful, but if you look at it http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html , it outlines the connection management method of HTTP components See section 2.11, which details how to use HTTP components to explicitly control connection management

Good luck

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>