Question

I am using the class HttpUrlConnection for requesting JSON responses

I realized that no matter if I set or not

System.setProperty("http.keepAlive", "false");

The first response is always going to take longer, while the next responses are very quick, with and without keepAlive. I am not even using SSL.

Notice that, my app doesn't need to perform any authentication with the server, so there isn't any startup call to the webservices. The first request I make to the webservices is actually the very first.

I am also verifying server-side with "netstat", that by setting keepAlive false on the Android client the connections disappear straight away, while without specifying keepAlive false they keep staying as "ESTABLISHED".

How can you explain that subsequent responses are quicker even if the connection doesn't persist?

ANDROID CODE:

line 1) URL url = new URL(stringUrl);
line 2) HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
line 3) InputStream instream = new BufferedInputStream(urlConnection.getInputStream());

Until line 2 everything always gets executed very quickly, either with keepAlive or not. Line 3 in the first request takes around 3 seconds, while in all subsequent always less than 1 second. Each request is about 0.5KB gzipped.

SYSTEM:

  • I am testing using a Nexus 5, connected via 3G
  • My webservices are written in Go, running on a CentOS 6.4 linux server
  • I am using standard tcp v4

UPDATE:

For the moment I have decided to use a trick: when the fragment is resuming, I make a HTTP HEAD request to the server. In this way all subsequent calls in the next 10 seconds are very quick. If the user waits more than 10 seconds then the first one will be slow, and the next ones will be quick again. This is all happening without using KeepAlive.

It's a really big mistery now. It looks like there is some kind of "awake" period which lasts for about 10 seconds. I don't think there is anything strange on my code which can result on that. Also because everything seems to happen during the line 3 I reported above.

SOLVED! thanks to Mark Allison!

Here is a very clear explanation: http://developer.android.com/training/efficient-downloads/efficient-network-access.html

Also, everything can easily be monitored using Android DDMS's Network Statistics. If you wait some seconds (let's say 20) from last request, you can see that it takes 2 seconds to transmit a new request.

Was it helpful?

Solution

I suspect that the lag that you are seeing is simply down to the cellular radio transitioning from either low power or idle state to full power (which can take over 2 seconds).

Check out Reto Meier's excellent DevBytes series on Efficient Data Transfer for an in-depth explanation of what's going on.

OTHER TIPS

  1. The first request cannot leverage a keep-alive obviously, because thankfully Android doesn't keep the connections alive for minutes or hours. Only subsequent requests can reuse keep-alive connections and only after a short period of time.

  2. It's natural that you have to wait in line 3. Before something like conn.getResponseCode() or conn.getInputStream() the HttpURLConnection is in CREATED state. There is no network activity until it's getting in CONNECTED state. Buffered* shouldn't make any difference here.

  3. I've observed long delays when using SSL and there was a time-shift between server and device. This happens very often when using an emulator which is not cold-booted. For that I've a small script running before test. It's important that PC and emulator are in the same time-zone, otherwise it's very contra-productive: (see below, because it's hard to show the command inline).

  4. I can imagine that Android saves battery in putting 3G into sleep mode when there is no activity. This is just speculation, but you could make a test by creating some other network activity with other apps (browser, twitter, ...) and then see whether your app needs the same long "think time" until first connection.

  5. There are other good options for losing time: DNS resolution, Server-side "sleep" (e.g. a virtual machine loading "memory" from disk).

The command to set time of Android emulator:

adb -e shell date -s `date +"%Y%m%d.%H%M%S"`

Edit

To further analyze the problem, you could run tcpdump on your server. Here is tutorial in case you don't know it well. Store the dumps to files (pcap) and then you can view them with wireshark. Depending on the traffic on your CentOS server you have to set some filters so you only record the traffic from your Android device. I hope that this gives some insight to the problem.

To exclude your server from being the bad guy, you could create a small script with curl commands doing the equivalent as your app.

You could create a super-tiny service without database or other i/o dependencies and measure the performance. I don't know "Go", but the best thing would be a static JSON file delivered by Apache or nginx. If you only have Go, then take something like /ping -> { echo "pong" }. Please tell us your measurements and observations.

Instead of using so many classes I suggest you use this library

you can have a look at here

http://loopj.com/android-async-http/

your code will become very very less , instead of declaring so many classes writing bulk of code , you can just use 4 lines of code

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
   }
});

It is very efficient in geting the response very quickly(1 or 2 secs including parsing).

I hope this will help you out. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top