Colas
Colas

Reputation: 187

HTTP requests with HttpClient too slow?

i'm trying to coding an android app that send some post values to a php file hosted at a dedicate server and store the array resoult

the code is this

   HttpPost httppost;
    DefaultHttpClient httpclient;

    httppost = new HttpPost("http://IP/script.php"); 
    HttpParams param = new BasicHttpParams(); 
    param.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);


  //  httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

    HttpProtocolParams.setContentCharset(param, "UTF-8");

    httpclient = new DefaultHttpClient(param);

    ResponseHandler <String> res=new BasicResponseHandler(); 
    List<NameValuePair> nameValuePairs;

    nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("id","1"));
    nameValuePairs.add(new BasicNameValuePair("api", "1"));


    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
Log.v("1",System.currentTimeMillis()+"");// Log to know the time diff
    String result= httpclient.execute(httppost, res);
Log.v("2",System.currentTimeMillis()+""); //  Log to know the time diff

this code waste about 2.5seconds (on 3G or WiFi) to send the post and get just "ok" string from server , even with good wifi this time down only to 2.2 / 2.0 seconds

I ran a simple Ajax sendpost script in my computer conected to internet through the same phone and 3G, it's take about .300ms to do the same stuff so ¿Same conection, same action, 2 seconds difference ?

///***UPDATE

I tried again my jquery script on my computer (with a mobile 3G+/HDSPA conection)

the average time response is about 250ms but always the first request up to 1.7secs, i tried to send posts with intervals of 30 seconds and i got 1.5 secs average time, then i tried to send a post with intervals of 2 seconds, the first was 1.41s and nexts 252ms

here you guys can view the chart: http://i46.tinypic.com/27zjl8n.jpg

This same test with cable conection (standard home DSL) offers always a fixed time response of ~170ms intervals regardless (not solid arguments here but IMHO maybe the first attempt is slightly slightly higher)

So there is something out (or wrong) severely affecting mobile conections in the first attempt, Any idea guys?

Upvotes: 4

Views: 16044

Answers (2)

firfor
firfor

Reputation: 169

you can use this command to test your network:

curl \
-X POST -T /path/to/file \
-o /dev/null -s -w "dns: %{time_namelookup}\ntime_connect: "%{time_connect}"\ntime_appconnect: "%{time_appconnect}"\ntime_pretransfer: "%{time_pretransfer}"\ntime_starttransfer: "%{time_starttransfer}"\ntime_redirect: "%{time_redirect}"\ntime_total: "%{time_total}"\n" https://your.server.name/xx

you may get some response like this:

dns: 0.005483
time_connect: 0.020602
time_appconnect: 0.062148
time_pretransfer: 0.062228
time_starttransfer: 0.062276
time_redirect: 0.000000
time_total: 0.175250

Your problem is probably because the first connection requires HTTPS, which takes a lot of time. If your subsequent request uses HTTP long links it will probably be much faster.

In a slow network, SSL connection will use more time to establish.

Upvotes: 0

MAJS
MAJS

Reputation: 86

Try using this configuration

HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, WAIT_RESPONSE_TIMEOUT);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);

This is the javadoc about setTcpNoDelay:

public static void setTcpNoDelay (HttpParams params, boolean value)

Since: API Level 1

Determines whether Nagle's algorithm is to be used. The Nagle's algorithm tries to conserve bandwidth by minimizing the number of segments that are sent. When applications wish to decrease network latency and increase performance, they can disable Nagle's algorithm (that is enable TCP_NODELAY). Data will be sent earlier, at the cost of an increase in bandwidth consumption.

Parameters

value true if the Nagle's algorithm is to NOT be used (that is enable TCP_NODELAY), false otherwise.

Upvotes: 7

Related Questions