Change variable in a task

Asked

Viewed 246 times

0

I have this method to read an html page, and it’s doing a toast with the result. But I wanted him to return the value read to me, or put it in some variable, but I did not succeed.

private class GrabURL extends AsyncTask<String, Void, Void> {
    private final HttpClient Client = new DefaultHttpClient();
    private String Content;
    private String Error = null;
    private ProgressDialog Dialog = new ProgressDialog(getActivity());

    protected void onPreExecute() {
        Dialog.setMessage("Carregando. Aguarde...");
        Dialog.show();
    }

    protected Void doInBackground(String... urls) {
        try {
            HttpGet httpget = new HttpGet(urls[0]);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            Content = Client.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            Error = e.getMessage();
            cancel(true);
        } catch (IOException e) {
            Error = e.getMessage();
            cancel(true);
        }

        return null;
    }

    protected void onPostExecute(Void unused) {
        Dialog.dismiss();
        if (Error != null) {
            Toast.makeText(getActivity(), Error, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "Source: " + Content, Toast.LENGTH_LONG).show();
            retorno = Content; // Essa variavel retorno é onde queria setar.
        }
    }

}

2 answers

1

It is a matter of timing. The variable retorno may even be receiving the value of Content, but the part of the code that is reading the value of retorno must be trying to access it before the AsyncTask finish running and therefore before it has the desired value. Remember: you cannot start running the AsyncTask and expect the value to be returned immediately to you, because the AsyncTask takes time to execute. For this there is the method onPostExecute(), which is executed just after doInBackground() have been completed and the requested data are ready and available.

What use will you make of retorno? Save it in the bank, in Shared Preferences, or display it in a TextView on screen? Whatever, the suggestion is to do it in the method itself onPostExecute(), which is the moment when she is at the value you want.

  • Thanks was that. Now came another question, I can control the maximum time to perform this function?

  • You have some alternatives to achieve this. If you want to know more details, open a new question in the Portuguese OS.

1


Remembering that for you to use the onPostExecute you must pass the kind of result on the inheritance.

1) Assuming it is a String, it is the third parameter:

extends AsyncTask<String, Void, String> 

2) Your Ackground function should return the type of this result, in the case of a String.

 protected String doInBackground(String... urls) {


         String result = "";

        return result ;
    }

3) Your onPostExecute function must be signed to receive a String as parameter:

protected void onPostExecute(String result)
  • Thanks dearly for the answer, now I ask you the same question I asked for Piovezan, I would have as I control the maximum time of execution of the function?

  • Yes, you would! All you have to do is implement a timer inside theInBackground. When it reaches the maximum time, you call the this.Cancel(true) function (true is to stop the processing even and not wait to finish). In this case, you must implement the onCancelled(String result) function so that you receive the obtained result until then. Here is an example of Timer: http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java

Browser other questions tagged

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