Android: HTTP requests in AsyncTask are not parallel

I am writing an application for fans of my local cinema , which shows the calendar for the next few days. The list of films for each day is retrieved using a parameterized HTTP call from the site (the answer contains Hebrew, so if you clicked on a link and got some gibberish it's probably OK).

The application displays the schedule for the next eight days, so it makes 8 calls requesting the schedule for the day.

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

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

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 running at the same time. Days load slowly one by one, which is very slow:

enter image description here

enter image description here

enter image description here

What is the best way to run these AsyncCalls at the same time?

+4
source share
2 answers

AsyncTask , as you know, gets into regression in Android 4.x, where the system executes them one at a time, and does not execute them at the same time, as it did with Android 1.6. This is by design: basically, on newer platforms, you can revert to the old parallel behavior by calling executeOnExecutor() instead of execute() . Mark Murphy (known as Commonsware at StackOverflow) has all the details sorted on his blog.

+11
source

Basically, AsyncTask creates only one thread for you, which runs in the background.

You can achieve what you want in two ways.

  • First, you can create separate AsyncTasks for each of the operations.
  • Secondly, by creating multiple threads in the doInBackground method and creating parallel requests and processes. After that, you can notify your user interface by publishing them to onProgressUpdate or onPostExecute.

check this

Android android and streaming

0
source

Source: https://habr.com/ru/post/1445302/


All Articles