Skip to content Skip to sidebar Skip to footer

App Not Working Fine On Ics Version Due To Networkonmainthreadexception

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 Ging

Solution 1:

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.

Solution 2:

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

Solution 3:

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:

privateclassDoNetworkStuffextendsAsyncTask<String, Integer, String> {

 protectedStringdoInBackground(String... params) {
     // Do network callif (!call.hasSucceeded()) returnnull;

     // Do next network callif (!call.hasSucceeded()) returnnull; // Check if call succeeded - pseudo code// Continue this pattern with all your network callsreturn""; // If this point is reached, all network calls succeeded
 }

 protectedvoidonPostExecute(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

Post a Comment for "App Not Working Fine On Ics Version Due To Networkonmainthreadexception"