0

I am running three AsyncTask parallel. (at least I think they are being executed parallel).

What I am doing is, I am using SAX Parser to parse data from three different XML feeds, the AsyncTask object is created three times inside a loop, each time with different URL.

for (int i = 0; i < URLs.length; i++){
                GetNews blog = new GetNews(i);
                blog.execute();
            }

When this code executes, three Asynctasks are executed. I want to know how to monitor each task separately, because I want to set the value of root Element, for example,

when Task1 is executed, rootElement = "entry", when Task2 is executed rootElement = "post"

and so on.. Here is what I have tried. for (int i = 0; i < URLs.length; i++){ if (i==0) rootElement = "entry" else if (i==1) rootElement = "post" else if (i==2) rootElement = "channel" GetNews blog = new GetNews(i); blog.execute(); }

but the entire code is executed in few seconds and the final rootElement value is set to the last one.

How can I monitor each task separately to set the value of the element for based on the task running.

Riley Willow
  • 594
  • 2
  • 5
  • 21
  • It's not answer on your question, but on post Honeycomb devices AsyncTasks run sequentially, see http://developer.android.com/reference/android/os/AsyncTask.html Order of execution section – Bracadabra Oct 25 '14 at 14:11

1 Answers1

0

Would something like this work for you?

ArrayList<GetNews> blogs = new ArrayList<GetNews>();

for (int i = 0; i < URLs.length; i++){
    blogs.add(new GetNews(i));
    blogs.get(i).execute();
}

// blogs now holds references to each running task
blogs.get(0).getStatus();
nPn
  • 16,254
  • 9
  • 35
  • 58
  • this wouldn't work actually, because the SAX Parser is executed inside AsyncTask, and as soon as your for loop block is executed, all three AsyncTask will start fetching Data from XML feeds, thus not giving the time to get status and setting the value of the element. – Riley Willow Oct 25 '14 at 13:31