Are HTTP requests with HttpClient too slow?

I am trying to encode an Android application that sends some post values ​​to a php file hosted on a dedicated server and saves a resoult array

code is

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 takes about 2.5 seconds (on 3G or WiFi) to send a message and get the string β€œnormal” from the server, even with good Wi-Fi this time up to 2.2 / 2.0 seconds

I launched a simple sending Ajax script on my computer connected to the Internet via the same phone and 3G, in order to do the same as in the case of the phone, it takes about 300 ms. So what is the same connection, the same action, 2 seconds difference?

/// *** UPDATE

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

the average time response is 250 ms , but always the first request is up to 1.7 seconds , I tried to send messages at intervals of 30 seconds, and I got an average value of 1.5 seconds time, then I tried to send a message at intervals of 2 seconds, the first was 1.41 s and nexts 252ms

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

The same cable connection test (standard home DSL) always offers a fixed response time of ~ 170 ms regardless (there are no good arguments here, but IMHO, perhaps the first attempt is a bit higher)

So, there is something (or wrong) that seriously affects mobile connections in the first attempt. Any ideas?

+4
source share
1 answer

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)

Starting at: Level 1 API

Determines whether the Nagle algorithm should be used. The niall algorithm attempts to conserve bandwidth by minimizing the number of segments that are sent. When applications want to reduce network latency and improve performance, they can disable the Nagle algorithm (that is, enable TCP_NODELAY). Data will be sent earlier, due to the cost of increasing bandwidth consumption.

Options

true if the Nagle algorithm should NOT be used (which allows TCP_NODELAY), otherwise false.

+7
source

Source: https://habr.com/ru/post/1434509/


All Articles