1

I am a new Android developer and I am trying to do the following in my app.

Given a URL, I want to get the html for that URL, parse for images in the HTML source and download them concurrently (may be 2-3 threads in parallel). I am doing the actual HTML/URL download in an Async Task in my Activity. After that I parse the HTML and I have the list of image urls. At this point, what is the best way to have multiple threads loop through these image urls and download them? Please suggest or provide an example. I tried looking for a blocking queue with multiple threads and also 'Looper' but am still confused on how to achieve this.

Thank you.

M99
  • 1,859
  • 10
  • 28
  • 50

2 Answers2

1

you probaly would use an AsyncTask, but avoid Javas Threads since the Android OS may terminate them as soon your App doesnt have a currently visible Activity. Also avoid to spin up multiple Tasks at once, since that usually wont make the download process faster. instead put your URLs in some kind of a task qeue

martyglaubitz
  • 992
  • 10
  • 21
  • Java `Thread`s are not automatically terminated in any way if the Activity is hidden. They behave the same way `AsyncTask` does, especially since `AsyncTask` is just `Thread` under the hood (or more technically a `FutureTask` executed by a `ThreadPoolExecutor`, see [sourcecodes](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/AsyncTask.java)). I would also consider testing if 2 or maybe 3 parallel download tasks do speed up things. It's not impossible, especially if you download from more than one server. – zapl Nov 13 '12 at 00:43
1

Take a Look at this, It is best Practice for your Problem and follow it, It will be good and best practice for you

http://android-developers.blogspot.in/2009/05/painless-threading.html

How to execute web request in its own thread?

Is there an accepted best-practice on making asynchronous HTTP requests in Android?

Community
  • 1
  • 1
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84