【发布时间】:2013-05-07 09:06:03
【问题描述】:
我有一个 android 应用程序,我从多个 url 中提取数据,然后保存为字符串的 arraylist。它工作正常,但要从 13 个 url 获取数据,需要将近 15-20 秒。在使用 phonegap 构建的同一个应用程序中,从同一组 url 获取数据需要 3-4 秒。这是下面的代码。
@Override
protected String doInBackground(String... params) {
client = new DefaultHttpClient();
for(int i=0;i<url.size();i++)
{
get = new HttpGet(url.get(i));
try {
response = client.execute(get);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
entity = response.getEntity();
InputStream is = null;
try {
is = entity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = null;
do {
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
buffer.append(line);
} while (line != null);
String str = buffer.toString();
param.add(str);
}
return null;
}
谁能建议我如何加快执行速度并减少提取时间。
【问题讨论】:
-
你可以并行执行...使用 HttpURLConnection 类
-
您的应用运行的 Android API 级别是什么?
-
@tactmayers,使用 14!
-
这种情况下,可能是
ASyncTasks没有并行运行导致性能下降。请参阅commonsware.com/blog/2012/04/20/… 了解更多信息
标签: android http-get androidhttpclient