1

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

Christopher
  • 13
  • 1
  • 6

1 Answers1

0

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

nonahex
  • 677
  • 1
  • 6
  • 9
  • I dont suppose you have any examples handy? I did what was mentioned in the following: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests and was getting the same kind of response times? thanks :D – Christopher Mar 07 '14 at 23:14