`
jiaoronggui
  • 浏览: 1302456 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
B7c2eb31-a8ea-3973-a517-d00141f39b89
项目管理软件-redmin...
浏览量:115182
4a63e153-250f-30f6-a051-97cfc67cb3d3
IT职业规划
浏览量:197636
社区版块
存档分类
最新评论

如何解决andriod的http连接需要花费2秒的问题

 
阅读更多
public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.1.137:8880/form");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
     Log.e(TAG,e.toString());
    } catch (IOException e) {
     Log.e(TAG,e.toString());
    }
}

 

通过以上的代码,打开一个http连接,花费了起码2秒钟,不知道为什么?正常的访问的话,只需要80ms就可以了,考虑下,采用HTTP1.1协议

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);

 

If I remember correctly, HTTP 1.0 opens a new TCP connection for every request. Does that explain the large delay?

如果我记得不错的话,http1.0协议为每个请求建立连接,这个也许可以解释为什么有这么长的延迟,

A HTTP POST request now takes between 50 and 150 ms over WLAN and something between 300 and 500 ms over 3G.

通常的话,一个post请求如果通过wlan网络请求的话大概需要花费50-150ms,如果通过3g可能需要300-500ms;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics