【问题标题】:Extracting data from server takes too much time- android从服务器提取数据需要太多时间 - android
【发布时间】: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


【解决方案1】:

您可以尝试从 for 循环中为每次迭代启动一个单独的线程。 像这样:

for(int i = 0; i < url.size(); i++){ 
    //start thread that gets data from url and adds it to the list
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2018-11-28
    • 2022-01-01
    • 2018-11-03
    • 2018-04-17
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多