Pull back from Asynctask class

Asked

Viewed 104 times

1

I have a class Main that performs a method receberSolicitantes() with a task in background. While it is executed, it updates a progressBar.

Everything is working perfectly with the method as follows:

public void receberSolicitantes() {
        JsonReceber receber = new JsonReceber(this, "solicitantes", this.progressBar);
        receber.execute();
    }

But when I put the line receber.get() (as shown below) to fetch the return, the method onProgressUpdate (asynchronous class JsonReceber) is only executed at the end of doInBackground, and thus, the progressBar doesn’t work.

Added receber.get():

public void receberSolicitantes() {
        JsonReceber receber = new JsonReceber(this, "solicitantes", this.pb);
        receber.execute();
        try {
            receber.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

How do I find the return of background execution ?

It would be the same way I do with progressBar leading on the call

JsonReceber receber = new JsonReceber(this, "solicitantes", this.progressBar) ?

1 answer

2


If you want the result of the task executed by Asynctask to be performed asynchronously then you cannot use the method get().

The method get() obtains the result but causes the execution of the program to wait for it to be calculated before continuing, so it is not asynchronous.

In order for the result to be calculated asynchronously you must use the method execute().
The result can be obtained in the method onPostExecute(), through the parameter declared therein.

See in the documentation how to use Asynctask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.