How to catch the return of the method ofInBackground on android?

Asked

Viewed 1,248 times

0

I’m starting to work with Asynctasks and I don’t know much yet. I created a class that extends Asynctask and implemented the method doInBackground(), but my question now is how do I get the return of this method? In another class in the method efetuarLogin() , I make a call for the method execute() , but it does not return me to String that should return. How can I get the return of the Ackground method? I thank you in advance for your cooperation!

protected String doInBackground(String... params) {

    String resposta = "";

    try {
        resposta = this.sendGet(params[0]);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return resposta;
}

Method calling class extending from Asynctask:

public Usuario efetuarLogin(String email, String senha){

    JSONStringer js = new JSONStringer();
    ConexaoHttp conexao = new ConexaoHttp();

    try {
        js.object();
        js.key("email").value(email);
        js.key("senha").value(senha);
        js.endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    String usuario = conexao.execute(js.toString()); // não retorna uma String, dá erro dizendo que ele retorna um objeto do tipo AsyncTask

    return null;
}

2 answers

2

The method doInBackground passes the result to another Asynctask method, the onPostExecute.

In your case, if Asynctask is created in Activity itself, you could assign the value of usuario within it:

void onPostExecute(String result) {
     usuario = result;
 }

The method execute in itself returns nothing. The ideal is to put the rest of the code you will use with this user also in the above method, because Asynctask runs in Background and the main thread only knows that it ended by this method.

More details about Asynctask, you can see here:

https://developer.android.com/reference/android/os/AsyncTask.html

1


If you are still facing this problem, a suggestion I used some time ago is to use the method get() of the instantiated object with the class it inherits from Asynctask just after invoking the method execute(). I confess that I cannot say if it is the most appropriate way, but I believe that at the moment, I can solve your problem. That way your code would look like this:

public Usuario efetuarLogin(String email, String senha){

    JSONStringer js = new JSONStringer();
    ConexaoHttp conexao = new ConexaoHttp();

    try {
        js.object();
        js.key("email").value(email);
        js.key("senha").value(senha);
        js.endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    conexao.execute(js.toString());
    String retorno = conexao.get(); // esse método retorna a resposta do método doInBackground()

    return retorno;
}
  • So you are using Asynctask synchronously. Asynctask’s get() method only returns when the Asynctask() method is finished.

Browser other questions tagged

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