Return String from onPostExecute ()

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 + ")");
}

MainActivity is sending two parameters (user and password) to class LoginBackground that checks through class SistemaHttp if there is a user registered with that data in the database. If it exists, the class SistemaHttp returns to doInBackground a String to "OK".
So I wanted to get this String and pass it to MainActivity to play it in RetornoLogin() method and display a Toast according to the answer that came. Note: this is all working up to onPostExecute , I put a Log () to display the return string of the doInBackground() and it shows OK normal, I just do not know to continue thereafter.

    
asked by anonymous 18.03.2016 / 17:58

1 answer

4

Declare an interface in the LoginBackground class:

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 onPostExecute() to:

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

Activity which uses LoginBackgroud to 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
}

When you create the LoginBackgroud instance, use your method setOnLoginCompletedListener() to "set" listener :

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

The onLoginCompleted() of activity will be called when onPostExecute() is executed.

    
18.03.2016 / 18:39