4

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?

Colas
  • 157
  • 1
  • 2
  • 12
  • 2 seconds is longer than it should be shorter than that. I've received responses in milliseconds. Can't say whats going wrong here just that the time should be better. – Nathan Schwermann Sep 16 '12 at 23:55
  • @Colas, It would have been nice if you mentioned that the "with 3G" part of your question was meaningless, since you have the same problem on WiFi as well. – Brad Sep 17 '12 at 00:39
  • @Colas, Set up your device to use a proxy server to see what is actually going on. Use Fiddler on your PC as that proxy server. – Brad Sep 17 '12 at 00:41
  • 1
    How often are you making the call to your service? The first time the call it made the device may have to wake up the 3g radios, and this can take a few seconds to do. Any calls made shortly after your first call should be faster. – Rob Sep 17 '12 at 01:02
  • @rob you seem to be right, the first post take about 2-3 secs to finish but nexts takes about 0.800s, if i wait 10 seconds beetween each post it take 3 seconds again.. or maybe this is http keep-alive related? – Colas Sep 17 '12 at 14:29
  • So any idea with the new information update? – Colas Sep 20 '12 at 22:08
  • Take a look at the information here that explains how phones connect to the network and how this is different from how it works on WLAN. What you are seeing is the connection to the GSM network being taken down and taking some time to be re-connected. http://developer.att.com/developer/forward.jsp?passedItemId=8100027 – Rod Burns Oct 05 '12 at 17:55

2 Answers2

7

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.

Tamás Cseh
  • 3,050
  • 1
  • 20
  • 30
MAJS
  • 86
  • 1
  • 5
  • not for me :( I am using org.apache.httpcomponents:httpclient:4.5.4 and setTcpNoDelay(true) makes no difference. Actually TcpNoDelay is set to true by default. I don't get why Apache Http Client is so slower than OkHttp library or the base http client released with golang – Alessio Santacroce Sep 28 '18 at 11:09
0

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.

user3033075
  • 139
  • 1
  • 6