Since the Internet connection may disappear at any time, why not just complete the task and handle the IOException, how does this happen? If there is no connectivity, I think creating an HTTP request or whatever you do, it worked immediately. Of course, you could add a check before you try, but such a check should not replace error handling during a network request.
I would probably put all the logic in doInBackground. If I first add a connection check, I would handle it the same way as any network exceptions that occur during the actual request. As for the actual handling of errors, you can save the error code as a member in your async, you could work in asynctask on a custom class containing both data and an error code, or you could even have doInBackground return null in case of an error and handle errors directly from doInBackground.
source share