Davood
Davood

Reputation: 5645

How to fetch data out of AsyncTask class in android?

I want to know how to access the data and bind it to a component in out of the AsyncTask class body?

I have a class like:

class DownloadData extends AsyncTask<String, Void, String> {....}

and it has a method :

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

        return ....;//return some data
    }

I don't understand doInBackground return data to where?

Because when i want to use my class, i use it like:

      DownloadData dd = new DownloadData();
            dd.execute(...);

can i use it like this? because i want to fetch returned data out of my main class to bind it to some components

      DownloadData dd = new DownloadData();
        string temp=dd.doInBackground(...);

Upvotes: 1

Views: 2477

Answers (3)

Aman Grover
Aman Grover

Reputation: 1641

If you just want to return a result from your AsyncTask class so that you can update the UI in your activity according to the result you can do something like this: In your AsyncTask class declare an interface like this:

private AsyncResponse asyncResponse = null;

public DownloadData(AsyncResponse as) {
    this.asyncResponse = as;
 }

public interface AsyncResponse {
    void onAsyncResponse(String result); // might be any argument length of any type
}

and in onPostExecute() :

asyncResponse.onAsyncResponse(result); // result calculated from doInBackground()

and in the activity class:

DownloadData dd = new DownloadData(new AsyncResponse() {...}); // implement onAsyncResponse here
dd.execute();

Upvotes: 0

Vyacheslav
Vyacheslav

Reputation: 27221

I can not catch

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

        return ....;//return some data
    }

result from main UI.

You have to use callbacks. For example, you can use interface to obtain the result.

For example create an interface:

public interface IProgress {
    public void onResult(int result);
}

create class:

     private class DownloadData extends AsyncTask<String, Void, String> {
    private IProgress cb;
    DownloadData(IProgress progress)  {
    this.cb = cb;
    }
@Override
protected String doInBackground(String... params) {


for (int i = 0; i < 10; i ++) {
if (cb!=nil)
cb.onResult(i);//calls 10 times
}
....
    }
...
    }

somewhere in code:

DownloadData dd = new DownloadData( new IProgress() {
public void onResult(int result) {
/// this is your callback

//to update mainUI thread use this:
final res = result;
runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //update UI here
textview.setText("" + res);
                }
            });

}
});
            dd.execute(...);

And, as usual, you can update UI after doInBackground via onPostExecute()

Upvotes: 0

buzzingsilently
buzzingsilently

Reputation: 1586

After doInBackground() your return will be forwarded to onPostExecute().

To use it in your activity refer this link : How to use Async result in UIThread

Upvotes: 4

Related Questions