Wrote a post request processing class via AsyncTask and accept the response from the server. How to pause the main thread to wait for AsyncTask to complete? When I call the Thread.currentThread.wait (int) method, I get the exception "InvocationTargetException"


It is necessary to make it so that the activity can process the result obtained from AsyncTask, and not be performed ahead of its completion.

  • googled a lot - little sense - jorik
  • Why then do asynctask if you want in the main thread? Do right in the main request, what's the problem? That's why he and asynctask not to wait until it is executed. - Yevgeny Suetin
  • The problem is that you cannot send a POST request to the server from the main thread. - Jorik
  • Then the answer is lower, this is the correct answer for your problem - Evgeny Suetin
  • The essence of the problem he does not solve - Jorik

2 answers 2

While AsyncTask running, show your application dialog with the ProgressBar , and the Loading... label, for example, over your application. And in the onPostExecute() method, handle the output of AsyncTask .


Never make the main thread hang. It will look like your program is frozen, and Android will offer to kill it.

  • A thought worthy, but not eradicating my problem. I need to process the AsyncTask result. I want to do this in the main thread. If I run the dialog activity for the time AsyncTask is running, the main activity code will not pause. Otherwise, I can just write the following code in onPostExecute, but I need to work in the main class. - Jorik
  • 3
    @Jorik, you can't do that. Revise your code to handle the result in onPostExecute . You should never hang the main thread under any reason. - Vladyslav Matviienko
  • @Jorik, I can’t think of any reason why you need to work, especially in the main class - Vladyslav Matviienko
  • 3
    @Jorik, unfortunately, I can only help with the right decision. With crushing crutches - alas, it is not tanu who can help. You will either have to do it right with our help, or make your own crutches yourself. - Vladyslav Matviienko
  • 3
    @Jorik, you are mistaken, I think. I think you need to redo the minimum. Starting an Activity in onPostExecute very simple: startActivity(new Intent(MainActivity.this, НоваяАктивити.class)); - Vladyslav Matviienko

The main thread cannot be stopped if the thread stops then the OS will consider that the application has hung and will kill it.