Pull AsyncTask class return

1

I have a Main class that runs a receberSolicitantes() method with a task in background . While it runs, 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 receber.get() line (as shown below) to fetch the return, the onProgressUpdate (asynchronous class JsonReceber ) method only runs at the end of doInBackground , and thus progressBar does not 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 search for background rendering?

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

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

    
asked by anonymous 22.02.2018 / 20:41

1 answer

2

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

The method get () gets the result but causes the execution of the program expect it to be calculated before continuing, and is therefore not asynchronous.

For the result to be calculated asynchronously, you must use the execute() method. The result can be obtained in the onPostExecute() method, through the parameter declared in it.

See in the documentation how to use AsyncTask .

    
22.02.2018 / 23:22