在应用程序启动时,我需要对不同的web服务(php)进行大约15次调用。
我在这篇文章中使用了以下代码
public static String post(String url, List<BasicNameValuePair>
postvalues, HttpClient httpclient) {
try {
if (httpclient == null) {
httpclient = new DefaultHttpClient();
}
HttpPost httppost = new HttpPost(url);
if ((postvalues == null)) {
postvalues = new ArrayList<BasicNameValuePair>();
}
httppost.setEntity(new UrlEncodedFormEntity(postvalues, "UTF-8"));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return requestToString(response);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String requestToString(HttpResponse response) {
String result = "";
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
result = str.toString();
} catch (Exception ex) {
result = "Error";
}
return result;
}问题是,一些请求必须以给定的顺序请求,每个请求需要大约1-2秒,因此“加载启动”大约需要10秒。
所以我的问题是:既然所有的连接都连接到同一台服务器,我如何才能改善这种延迟?有没有办法打开一个连接,并通过这个“隧道”发送所有的请愿书,以减少延迟?
注意:我测试了代码,并且在每个连接中使用新的httpclient重用请求花费了相同的时间
谢谢
发布于 2012-05-08 20:12:06
https://stackoverflow.com/questions/10497560
复制相似问题