的HTTP POST请求使用的HttpClient需要2秒,为什么呢? [英] HTTP Post requests using HttpClient take 2 seconds, why?

查看:343
本文介绍了的HTTP POST请求使用的HttpClient需要2秒,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新: 找到自己的答案,见下图: - )

您好,

我'目前编码一个Android应用程序,提交材料使用HTTP POST和AsyncTask的背景。我用的是<一href="http://developer.android.com/reference/org/apache/http/client/package-summary.html">org.apache.http.client包这一点。我根据我的code对这个例子的。

基本上,我的code是这样的:

 公共无效POSTDATA(){
    //创建一个新的HttpClient和门柱头球
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(http://192.168.1.137:8880/form);

    尝试 {
        名单&LT;的NameValuePair&GT; namevaluepairs中=新的ArrayList&LT;的NameValuePair&GT;(2);
        nameValuePairs.add(新BasicNameValuePair(ID,12345));
        nameValuePairs.add(新BasicNameValuePair(StringData是,AndDev就是酷!));
        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

        //执行HTTP POST请求
        HTT presponse响应= httpclient.execute(httppost);

    }赶上(ClientProtocolException E){
     Log.e(TAG,e.toString());
    }赶上(IOException异常E){
     Log.e(TAG,e.toString());
    }
}
 

问题是,在 httpclient.execute(..)线大约需要1.5〜3秒,我不明白为什么。只是请求一个页面的HTTP GET大约需要80毫秒左右,所以这个问题似乎并没有成为网络延迟本身。

这个问题似乎并没有在服务器端要么,我自己也尝试过账数据,以 http://www.disney.com / 中与同样缓慢的结果。而萤火虫显示发送的数据到我的服务器在本地时1毫秒响应时间。

这发生在模拟器和我的Nexus One(均与Android 2.2)。

如果你想看看完整的code,我已经把它放在<一个href="http://github.com/pableu/http-post-example/blob/master/src/com/gaga/project/newA.java">GitHub.

这只是一个虚拟的程序做HTTP邮政使用的AsyncTask在按下按钮的背景。这是我的第一个Android应用程序,而我的第一个Java code很长一段时间。而incidentially,也是我在#1的第一个问题; - )

任何想法,为什么httpclient.execute(httppost)需要这么长时间?

解决方案

还好吧,我解决了这个自己多带些调查。我所要做的就是补充一点,设置HTTP版本为1.1的参数,如下所示:

 的HttpParams PARAMS =新BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpClient的HttpClient的=新DefaultHttpClient(PARAMS);
 

我发现这多亏了非常漂亮的<一个href="http://$c$c.google.com/p/and-bookworm/source/browse/trunk/src/com/totsp/bookworm/data/HttpHelper.java">HttpHelper类的从和-蠹和一些试验和错误。

如果我没有记错,HTTP 1.0打开为每个请求一个新的TCP连接。这是否说明大延误?

一个HTTP POST请求,现在只需50至150毫秒以上WLAN和300至500毫秒之间的事情了3G。

Update: Found the answer myself, see below :-)

Hi,

I'am currently coding an android app that submits stuff in the background using HTTP Post and AsyncTask. I use the org.apache.http.client Package for this. I based my code on this example.

Basically, my code looks like this:

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());
    }
}

The problem is that the httpclient.execute(..) line takes around 1.5 to 3 seconds, and I do not understand why. Just requesting a page with HTTP Get takes around 80 ms or so, so the problem doesn't seem to be the network latency itself.

The problem doesn't seem to be on the server side either, I have also tried POSTing data to http://www.disney.com/ with similarly slow results. And Firebug shows 1 ms response time when POSTing data to my server locally.

This happens on the Emulator and with my Nexus One (both with Android 2.2).

If you want to look at the complete code, I've put it on GitHub.

It's just a dummy program to do HTTP Post in the background using AsyncTask on the push of a button. It's my first Android app, and my first java code for a long time. And incidentially, also my first question on Stackoverflow ;-)

Any ideas why httpclient.execute(httppost) takes so long?

解决方案

Allright, I solved this myself with some more investigation. All I had to do was to add a parameter that sets the HTTP Version to 1.1, as follows:

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

I found this thanks to the very nice HttpHelper Class from and-bookworm and some trial-and-error.

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

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

这篇关于的HTTP POST请求使用的HttpClient需要2秒,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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