Android:AsyncTask中的HTTP请求不是并发的 [英] Android: HTTP requests in AsyncTask are not concurrent

查看:218
本文介绍了Android:AsyncTask中的HTTP请求不是并发的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为)具有所有详细信息


I'm writing a fan app for my local cinematheque, which shows the screening calendar for the next few days. The per-day film list is retrieved using a parametrized HTTP call from the site (The answer contains Hebrew, so if you clicked the link and got some Gibberish it's probably OK).

The app displays the schedule for the next eight days, so It makes 8 calls with per-day schedule request.

private class GetMoviesTask extends AsyncTask<Integer, Void, List<Film>>

doInBackground() retrieves the list of films per day, and onPostExecute() updates the interface.

The AsyncTask is called from MainActivity.onCreate():

for (int i=0; i<NUMBER_OF_DAYS_TO_VIEW; i++){
    new GetMoviesTask().execute(i);
}

The problem is that AsyncTask is not executed concurrently. The days are slowly loaded one after another, which is painfully slow:

What's the best way to start these AsyncCalls concurrently?

解决方案

AsyncTask has been known to hit a regression in Android 4.x, where the system executes them one at a time instead of executing them concurrently as it did since Android 1.6. This is by design: basically, on newer platforms, you can revert to the old concurrent behaviour by calling executeOnExecutor() instead of execute(). Mark Murphy (known as Commonsware on StackOverflow) has all the details sorted on his blog.

这篇关于Android:AsyncTask中的HTTP请求不是并发的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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