用HttpClient发出的HTTP请求太慢了吗? [英] HTTP requests with HttpClient too slow?

查看:1427
本文介绍了用HttpClient发出的HTTP请求太慢了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编码一个Android应用程序,该应用程序将一些后值发送到专用服务器上托管的php文件并存储数组结果

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

代码是这个

   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

此代码浪费了大约2.5秒(在3G或WiFi上)发送帖子并从服务器获取 ok字符串,即使这次使用良好的wifi,这次也仅降至2.2 / 2.0秒

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

我在我的服务器上运行了一个简单的Ajax sendpost脚本通过相同的电话和3G连接到互联网的计算机,大约需要花费300毫秒才能完成相同的操作,所以„相同的连接,相同的动作,相差2秒?

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 + / HDSPA连接)再次尝试了我的jquery脚本

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

平均时间响应约为 250毫秒,但总是首次请求最多1.7秒,我尝试发送间隔为30的帖子秒,平均时间为1.5秒,然后我尝试发送间隔为2秒的帖子,第一次是1.41s,然后是252ms

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

在这里您可以查看图表: http://i46.tinypic.com/27zjl8n.jpg

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

使用电缆连接的同一测试(标准的家用DSL)始终提供〜170ms 间隔的固定时间响应(此处不是可靠的参数,但恕我直言,也许第一次尝试会稍高一些)

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?

推荐答案

尝试使用此配置

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

这是关于 setTcpNoDelay 的Javadoc:

This is the javadoc about setTcpNoDelay:

public static void setTcpNoDelay(HttpParams参数,布尔值)


自:API级别1

Since: API Level 1

确定是否要使用Nagle的算法。 Nagle的
算法试图通过最小化发送的
段的数量来节省带宽。当应用程序希望减少网络
的延迟并提高性能时,可以禁用Nagle的算法
(即启用TCP_NODELAY)。数据将更早发送,带宽消耗增加

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如果不使用Nagle的算法,则为true(即启用
TCP_NODELAY),否则为false。

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

这篇关于用HttpClient发出的HTTP请求太慢了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆