-2

I have developed an Android App which will go live in few days.

I have come across a different problem when testing the app on ICS. It is working fine on Pre-GingerBread and GingerBread version devices. It is throwing NetworkOnMainThreadException when tested ICS device and managed to remove that by putting HTTP Calls in AsyncTask.

But my issue is I am making 4 to 5 different HTTP Url calls and calling every url require a AsyncTask as these URLs should execute concurrently one after other.

If result of one url is failed next one should not be called and that should updated on UI. So I am taking an AsyncTask for each URL Call.

Can we have many AsyncTasks in a Single Fragment or not. As of I Managed to have Maximum 3 AsyncTask in my Fragment Class.

If so can we start executing another AsyncTask from first AsyncTask PostExecute method.

Thanks in Advance.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
Nagaraj436
  • 33
  • 1
  • 5

3 Answers3

2

You can put all the HTTP calls in one ASyncTask and report back with the publishProgress callback. When one fails, you can stop that ASyncTask and the others won't be called.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
1

It should be possible to check the URL one after another in one asyncTask and publish back the progress via:

http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate%28Progress...%29

sweisgerber.dev
  • 1,726
  • 17
  • 36
  • 1
    This questions is related to and answered in: [Question 2492909](http://stackoverflow.com/questions/2492909/asynctask-rejectedexecutionexception-and-task-limit) – sweisgerber.dev Aug 10 '12 at 09:26
1

A NetworkOnMainThreadException is generally a sign of poor code, and it'll result in a bad user experience because the application locks up whenever it's running some sort of network activity. Yes, it (unfortunately) works on pre-Honeycomb devices, but it's not what anyone should be going for.

To solve your problem, I'd highly recommend you use a single AsyncTask and do all your HTTP calls successively in the doInBackground() method. For each call, check if the call succeeded. If it did, then it'll just move on to the next one - if it fails, you can just return null after which no further HTTP calls will be made, and the onPostExecute() method will be called automatically. Quite simple.

Here's an example of what I have in mind:

private class DoNetworkStuff extends AsyncTask<String, Integer, String> {

 protected String doInBackground(String... params) {
     // Do network call
     if (!call.hasSucceeded()) return null;

     // Do next network call
     if (!call.hasSucceeded()) return null; // Check if call succeeded - pseudo code

     // Continue this pattern with all your network calls

     return ""; // If this point is reached, all network calls succeeded
 }

 protected void onPostExecute(String result) {
     if (result != null) {
         // All calls succeeded - celebrate!
     } else {
         // A call failed - clean-up...
     }
 }

}

Take a look at the documentation for AsyncTask and understand it before doing anything else. I'm sure this will work perfectly for your application: http://developer.android.com/reference/android/os/AsyncTask.html

Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • First of all, don't post the same comment to all three answers. You can have as many AsyncTasks as you want, but it is NOT needed. Everything you need to know is in my answer. – Michell Bak Aug 10 '12 at 10:27