Yossale
Yossale

Reputation: 14361

Checking how long a request from mobile device to server takes

I'm sending an http request from my mobile Android app to a server, and I want to print out how long the upload took. How can I do that?

Just printing out system time before and after the request won't help, because it also includes the time it took the server to answer.

This is my current request code:

HttpPost httppost = getHeader(uri, 0);    //fileSize);
httppost.setEntity(reqEntity);
HttpResponse response = null;
httpclient = new DefaultHttpClient();

Log.i(TAG, "start execute");
response = httpclient.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (code != 200) {
    Log.i(TAG, "http error! : " + code);
    throw new HttpException("Server returns with http error (" + code + ")");
}

Upvotes: 0

Views: 316

Answers (2)

Khawar Raza
Khawar Raza

Reputation: 16120

Simple save time before sending HTTP request and save time when it returns and then their difference will be the time taken by request to complete.

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

Here is a flow:

  • Send to the server Android current time.
  • Server should store time offset between server time and android time
  • Next, Send your request where one of parameters should be time when Android sent message.
  • Server will take his local time, add offset between server time and android time and calculate receive time.

Easy, right?

============================================================

Lets say android has time 14:23:01:010 Server time 14:23:15:000

Android sends request with his local time and server store offset (14:23:15:000) - (14:23:01:010) = 13.990 sec.

Now Android sends the message when local time was 14:24:01:000 and server got message when HIS local time was 14:24:16:050.

So (14:24:16:050) - (14:24:01:000) = 15.050 sec.

Now remove offset:

15.050 - 13.990 = 1.06 sec

This is your request time

Upvotes: 1

Related Questions