0

In my Android application, I use to call more AsyncTasks in order to execute REST requests to a server (GET/POST to retrieve images, and other informations as real-time feed). Imagine a scenario like this: I have a main activity with a ListView and an associated Adapter showing a lot of views having images. These images are loaded dinamically from cache or if missing from web through a REST request to a server. The request operate as follow: -the app instantiate a new AsyncTask having in doInBackground a POST request (for example) with HttpUrlConnection class. When the response is ready and retrieved I execute a callback to the java class that needs the info.

The problem is present when a request is waiting for result and the other requests are freezed for completion of the first. Imagine that the server take time to elaborate a request and a set of images needs to be loaded. The first take a while and block the loading of other images taking GUI incomplete.

How can I manage simultaneous tasks with REST requests? Is AsyncTask the right way to operate or should I use something else?

Thank you

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Claudio P
  • 81
  • 2
  • 9
  • 1
    starting form some Android version (2.3 probably) all AsyncTasks are executed on a single background thread (one by one therefore). You should execute them on custom executor – Vladyslav Matviienko Apr 05 '18 at 08:40
  • 1
    Possible duplicate of [Running multiple AsyncTasks at the same time -- not possible?](https://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible) – Vladyslav Matviienko Apr 05 '18 at 08:40
  • but anyway I'd recommend you to use one of the popular image loading libraries (Picasso, Glide) to load images instead of creating your own way. – Vladyslav Matviienko Apr 05 '18 at 08:41

0 Answers0