Return onPostExecute() String

Asked

Viewed 656 times

1

I have this code:
Mainactivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
...

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String user = txt_usuario.getText().toString();
            final String pssw = txt_senha.getText().toString();

            //progressBar.setVisibility(View.VISIBLE);

            LoginBackground lb = new LoginBackground();
            lb.execute(user, pssw);
        }
    });
}

private void retornoLogin(String ret) {

    if (ret.equals("OK"))
    {
        Toast.makeText(this, "Logado com sucesso", Toast.LENGTH_SHORT).show();
    }
    else if (ret.equals("ERRO"))
    {
        Toast.makeText(this, "Login incorreto", Toast.LENGTH_SHORT).show();
    }

}

And this:

Loginbackground:

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

    String p1 = params[0];
    String p2 = params[1];

    SistemaHttp sHttp = new SistemaHttp(null);
    String logado = sHttp.retornaUsuario(p1, p2);

    return logado;
}

@Override
protected void onPostExecute(String str)
{
    Log.d("TESTE","(LoginBackground) onPostexecute(" + str + ")");
}

To MainActivity sending two parameters(user and password) to the class LoginBackground which checks through the class SistemaHttp if there is a registered user with this data in the database. If there is one, the class SistemaHttp returns to the doInBackground a String to "OK".
So I wanted to take this String and pass it on to the MainActivity to throw it in the way RetornoLogin() and display a Toast according to the answer that came. Obs: everything is working until the onPostExecute, I put a Log() to display the doInBackground() and it displays normal OK, just do not know proceed from then on.

  • To Asynctask is an inner class of Activity?

  • I’m sorry I’m a beginner in java(android) as well as activty internal?

  • You mean if you’re in the same class?

  • If not, I have a Mainactivity class (with the button) calling the other Loginbackground class (which extends Asynctask and has its respective methods from Background, onPostExecute) which checks if the user exists with the Systemshttp class. I don’t know if that’s what you asked me.

1 answer

4


Declare an interface in the class Loginbackground:

public interface OnLoginCompletedListener{
    void onLoginCompleted(String result);
}

Add an instance variable:

private OnLoginCompletedListener onLoginCompletedListener;

Declare a new method:

public void setOnLoginCompletedListener(OnLoginCompletedListener onLoginCompletedListener){
    this.onLoginCompletedListener = onLoginCompletedListener;
}

Change the onPostExecute() for:

@Override
protected void onPostExecute(String str)
{
    if(onLoginCompletedListener != null){
       //Chama o listener passando a string
        onLoginCompletedListener.onLoginCompleted(str);
    }
}

Make the Activity who uses Loginbackgroud implement the interface OnLoginCompletedListener

Implement the interface:

@Override
void onLoginCompleted(String result){

    //Chame o método retornoLogin()
    retornoLogin(result);

    //Ou se preferir passe para a aqui o seu código
}

After creating the instance of Loginbackgroud use your method setOnLoginCompletedListener() for "set" the Listener:

-----
LoginBackground lb = new LoginBackground();
lb.setOnLoginCompletedListener(MainActivity.this);
lb.execute(user, pssw);

The method onLoginCompleted() of Activity will be called when onPostExecute() be executed.

  • Thanks @ramaral, I was breaking my head with this and as I don’t understand almost anything of java(android) I get lost without knowing what to do.

  • This is one of the most widely used approaches for class communication.

  • I’ve done the rereading of onPostExecute class AsyncTask and some tests. I think that’s my problem, the lack of an interface. Only the onPostExecute would not work, because I need to check at the end to be able to call the next Activity if login is ok. In another case, I used the return by .get(), but that way it did not show the progressBar. That would be my confusion with the method?

  • I spent a lot of time researching, and I’m still on that idea. Now I really don’t know what to do, whether I’m right or wrong.

  • @Rbz If this answer is not what you seek, then I still don’t understand what your doubt is.

  • @ramaral So I believe this is the answer, use an interface. The doubt is still as to what I said, that I had not understood the method onPostExecute, and I still do not understand what was the "reference", if I am leaving something behind... rs

Show 1 more comment

Browser other questions tagged

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