How to return global variable within a RequestTask class

1

Well I use the RequestTask class to do some things like:

  • protectedInBackground String (String ... uri)
  • protected void onPostExecute (String result)

I just want to return the variables to do other things outside of the RequestTask class, I read something about the scope of variables but I do not know how to do it.

Ex:

public class Inicio_Activity ...{

// minha classe requesttask..
class RequestTask extends AsyncTask<String, String, String>{

...meus comandos...

teste = "Pronto";

}

// declaro a string
String teste = "";

// Oncreate
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

new RequestTask().execute(http://www.asdasdas.com);

// digamos que tenho um textview que ira receber essa variável onde deveria aparecer "Pronto", como minha classe não retorna essa variável, não ira exibir nada.
texto1.setText(teste);

}
    
asked by anonymous 19.10.2014 / 22:47

2 answers

1

You're in front of a bigger Android problem: the Async concept. On the contrary of iOS that authorizes to block the user, Android, with the Async system, does not allow. Even if you use a global variable in Async, it will change little because Async works the way .... Async! It means that nothing can show that when you're going to want to use the global variable, it will have the result you're expecting.

You have 2 options:

1) create a "CallBack" and do the entire treatment within Async

2) Force Async to wait (not good in Android spirit !!)

Example Callback with an HTTP Request:

public Biblio_HTTP(OnHttpTaskCompleted listener)
{
    super();
    this.listener = listener;
}

@Override
protected void onPreExecute()
{
    super.onPreExecute();
}

@Override
protected String doInBackground(String... url) {
    String text =null;
   <CUT>
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);

}


@Override
protected void onPostExecute(String result) {
    listener.OnHttpTaskCompleted(result);    // Callback
}

// Def do Callback
public interface OnHttpTaskCompleted {

    void OnHttpTaskCompleted(String result);
}
}

  public class Callback implements Biblio_HTTP. OnHttpTaskCompleted {
    @Override

    public void OnHttpTaskCompleted(String result) {

        // Aqui vc faz o que vc quer com o resultado
    }
}

Example 2 with a 'blocked' alert:

    AlertDialog alert = builder.create();
    alert.show();
    try { Looper.loop(); }
    catch(RuntimeException e2) {}

    return resultado_alert;
    
20.10.2014 / 13:23
0

In the RequestTask constructor, pass the Text1 textText1 parameter, and in the onPostExecute () method, place the text1.setText ("yourText") command line.

Example:

public RequestTask(TextView texto) {
        this.texto = texto;
    }

@Override
    protected void onPostExecute() {
        texto1.setText("seu texto");
    }
    
20.10.2014 / 12:39