Why do I get null on onPostExecute?

Asked

Viewed 49 times

0

This code is only returning null the Return, how can I fix it?

@SuppressWarnings("deprecation")
public class MecanismoString extends AsyncTask <String, String, String> {

    @SuppressLint("StaticFieldLeak")
    private Context context;
    private Interface execinterface;

    private String retorno;

    MecanismoString (Context context, Interface execinterface){

        this.context = context;
        this.execinterface = execinterface;
    }

    @Override
    public String doInBackground(String... params) {

            Ion.with(context)
                    .load(params[1])
                    .setBodyParameter("login", params[2])
                    .setBodyParameter("senha", params[3])
                    .asJsonObject()
                    .setCallback(new FutureCallback<JsonObject>() {
                        @Override
                        public void onCompleted(Exception e, JsonObject resultado) {

                                retorno = resultado.get("perfil").getAsString();

                        }
                    });

        return retorno;

    }

    public void onPostExecute(String retorno) {

        execinterface.carregarString(retorno);

    }
}

when I use debug to read the code

retorno = resultado.get("perfil").getAsString();

it returns the correct bd value, but onPostExecute only reads null..

  • Dear Tiaho, this seems to me Java does not https://ion-lang.org, or did I get something wrong?

  • @Guilhermenascimento Ion, in this case, is a lib/api

  • @ramaral that’s right, I think I can understand that the title of the question sounds strange, since it is an API has no sense to write "Ion language", agree? And the lack of tag on the question can still confuse more. I understand that we do not have the tag as in Soen: https://stackoverflow.com/questions/tagged/ion to have a wiki and a brief description, but the biggest problem is the beginning of the title ;)

1 answer

1

The use of Asynctask, in this case, makes no sense, the processing done by the api Ion is already asynchronous.

The result that comes to onPostExecute() is null because the line return retorno;, of the method doInBackground(), is executed before the method onCompleted(). Remember that onCompleted() is called asynchronously by Ion.

When does the debug works because the process of debug is slow, giving Ion time to process and call the method onCompleted() before the Return is executed.

Change the code to only use the Ion.with().

  • I will test, I will study further sinking the ion to make this change, the asynk in this case is not necessary, my intention was to understand for in the other Activity call several separate connections...

Browser other questions tagged

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