Apache httppost Android webservice return 大量 XML 需要这么长时间

Apache httppost Android webservice return large number of XML takes times so long

我正在使用 apache httppost 从服务器中的 ASP.NET 网络服务获取 XML 数据。

当xml条记录数在千条以下时(例如400条xml)处理时间相当快,大约10秒,但当记录条数为数千条时(例如4000条xml) 处理时间大约是 20 分钟,这太疯狂了。

应该是 100 秒 ((4000/400) * 10) = a.k.a 1 分 40 秒

我正在尝试使用 limit 和 offset 拆分我的查询,并将 httppost 执行放在循环内,但它仍然需要相同的时间

httpParameters = new BasicHttpParams();
httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);       
HttpConnectionParams.setTcpNoDelay(httpParameters, true);


httpclient = new DefaultHttpClient(httpParameters);     
httppost = new HttpPost("http://myipaddress/servicename.asmx/" + wsFunctionName);       
httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i = 0 ; i < paramname.length; i ++){
    nameValuePairs.add(new BasicNameValuePair(paramname[i], para[i]));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));           
httpresponse = httpclient.execute(httppost);
HttpEntity entity = httpresponse.getEntity();
contentlen = (int) entity.getContentLength();               
InputStream is = entity.getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

String line = "";
try {
    while ((line = rd.readLine()) != null) {
        result += line;
        progressvalue += line.length();
        prog = (int) ((progressvalue * 100) / contentlen );
        publishProgress(new Integer[]{prog, in+1});
    }   
} catch (Exception e) {
    Log.e("e", e.toString());
} finally {
    is.close();
    entity.consumeContent();
}

注意:以前我没有使用

httpParameters.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

httppost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);

代码但是处理时间太长了(没有区别)

编辑:

问题不在于连接,而是来自 XML return 的行数太大,因此 readLine 进程一直持续到行结束。

我已将我的 XML return 从字符串列表 [](其中有很多换行符)更改为字符串。以前大约需要20分钟,现在只需要5秒,因为只需要读一行。

编辑 2:

测试结果真棒,

使用常规字符串+= "string"需要14秒,使用StringBuilder只需要2秒

感谢 greenapps

问题是大量的字符串连接。

我们应该使用 StringBuilder 而不是普通运算符 +=