-5

In my Android app, I sometimes need to wait the end of an AsyncTask which connect to Network, to have results, and carry on after, depending of the value of the results! That's what we call Synchronous Task without the A before synchronous. But how do I do that: I mean if I don't do it in an AsyncTask it return me an android.os.NetworkOnMainThreadException (whereas it is what I want to do), and an AsyncTask can't return results!!!

Everybody has an answer?

Thank you very much!

ImFabien75
  • 157
  • 1
  • 9

2 Answers2

2

I mean if I don't do it in an AsyncTask it return me an android.os.NetworkOnMainThreadException

No. If you don't do it in a background Thread (which AsyncTask is one way of doing so) it will return the exception.

Painless Threading

and an AsyncTask can't return results!!!

Incorrect. AsyncTask can return results through onPostExecute(). which is explained pretty well in The Documentation

I sometimes need to wait the end of an AsyncTask which connect to Network, to have results, and carry on after, depending of the value of the results!

Ok, if there is no UI work to be done, show a ProgressDialog while fetching the results. Then use onPostExecute() to handle the next task depending on the results.

This answer explains how to get results once the task has finished.

Also, How to get result of onPostExecute()

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
2

Everybody has an answer?

Yes. Do not do networking on the main thread. Period.

Use async task and onPostExecute() of async task is exactly for doing work after the doInBackground() finished. Also onPostExecute() is running on UI thread. All this is nicely documented.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141