eclipse 使用 HttpClient 的 HTTP 请求太慢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12451687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP requests with HttpClient too slow?
提问by Colas
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
我正在尝试编写一个 android 应用程序,该应用程序将一些 post 值发送到托管在专用服务器上的 php 文件并存储数组结果
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
这段代码浪费了大约 2.5 秒(在 3G 或 WiFi 上)发送帖子并从服务器获取“ok”字符串,即使这次使用良好的 wifi 也只需要 2.2 / 2.0 秒
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 ?
我在通过同一部手机和 3G 连接到互联网的计算机上运行了一个简单的 Ajax sendpost 脚本,做同样的事情大约需要 0.300 毫秒吗?同样的连接,同样的动作,2 秒的差异?
///***UPDATE
/// ***更新
I tried again my jquery script on my computer (with a mobile 3G+/HDSPA conection)
我在我的电脑上再次尝试了我的 jquery 脚本(使用移动 3G+/HDSPA 连接)
the average time response is about 250msbut 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
的平均时间应答是大约250毫秒,但总是最多1.7secs第一请求,我试图以30秒的间隔发送讯息和我得到1.5秒的平均时间,然后我试图发送一则讯息以2秒的间隔,第一是 1.41 秒,接下来是 252 毫秒
here you guys can view the chart: http://i46.tinypic.com/27zjl8n.jpg
在这里你们可以查看图表:http: //i46.tinypic.com/27zjl8n.jpg
This same test with cable conection (standard home DSL) offers always a fixed time response of ~170msintervals regardless (not solid arguments here but IMHO maybe the first attempt is slightly slightly higher)
使用电缆连接(标准家庭 DSL)进行的相同测试始终提供约 170 毫秒间隔的固定时间响应(这里不是可靠的论据,但恕我直言,第一次尝试可能略高)
So there is something out (or wrong) severely affecting mobile conections in the first attempt, Any idea guys?
因此,在第一次尝试中,有一些东西(或错误)严重影响了移动连接,有什么想法吗?
回答by MAJS
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:
这是关于setTcpNoDelay的 javadoc :
public static void setTcpNoDelay (HttpParams params, boolean value)
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.
从:API 级别 1
确定是否使用 Nagle 算法。Nagle 算法试图通过最小化发送的段数来节省带宽。当应用程序希望减少网络延迟并提高性能时,它们可以禁用 Nagle 算法(即启用 TCP_NODELAY)。数据将更早发送,代价是带宽消耗增加。
参数
如果不使用 Nagle 算法(即启用 TCP_NODELAY),则值为 true,否则为 false。