简体   繁体   中英

Increase Speed of HTTPClient Connections Android

I have been searching around for hours now trying to find an answer to this but I cant seem to get anything.

In an android app I am making, I am trying to connect to a website and get its headers. From each page I need getStatusCode() and the response time. I can do this, but the problem is that it takes ages. Each connection is taking between 0.7-3seconds. I have tried a load of sites so its not just the servers problems. The code is as below:

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
HttpResponse response;
String responseString = null;

long startTime = System.currentTimeMillis();
response = httpclient.execute(new HttpGet(site));
long elapsedTime = System.currentTimeMillis() - startTime;
Log.v("debugging",elapsedTime+" ");

From what I have been reading, alot of people have been complaining about the speed. Some have said that specifying the http version makes it faster but that hasnt changed anything. Does anyone know a way to make this faster or a quicker method.

Thanks

Recenty I had same problem, especially on older devices with Android 2.3.x request and response took over 3 seconds. After use HttpURLConnection, response time is about 1 seconds, depending on size of response. On nexus 4, time for request and response took approximately same time as desktop browser does.

http://developer.android.com/reference/java/net/HttpURLConnection.html

edit Try example from this http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ . It helped me a lot.

And now I remembered, that I have done one more thing, that helped on older devices. If your server uses GZIP compression, try to disable it.

urlConnection.setRequestProperty("Accept-Encoding", "identity");

Source: http://developer.android.com/reference/java/net/HttpURLConnection.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM