Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top