How to use AsyncTask

16,158

FAQs and general explaination of the usage of AsyncTask

=> Where should I do network operations? Where should I return my aquired values?

In general, you should do Network Operations in a Seperate Thread -> doInBackground(); since you do not want your UI to freeze when a Network Operation takes its time. So you should connect to your Service or .php script or wherever you get the Data from inside the doInBackground() method. Then you could also parse the data there and return the parsed data from the doInBackground() method by specifying the return type of doInBackground() to your desires, more about that down there. The onPostExecute() method will then receive your returned values from doInBackground() and represent them using the UI.

=> AsyncTask< String, Integer, Long> ==> How does this work?

In general, the AsyncTask class looks like this, which is nothing more than a generic class with 3 different generic types:

AsyncTask<Params, Progress, Result>

You can specify the type of Parameter the AsyncTask takes, the Type of the Progress indicator and the type of the result (the return type of doInBackGround()).

Here is an Example of an AsyncTask looking like this:

AsyncTask<String, Integer, Long>

We have type String for the Parameters, Type Integer for the Progress and Type Long for the Result (return type of doInBackground()). You can use any type you want for Params, Progress and Result.

private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {

 // these Strings / or String are / is the parameters of the task, that can be handed over via the excecute(params) method of AsyncTask
 protected Long doInBackground(String... params) {

    String param1 = params[0];
    String param2 = params[1];
    // and so on...
    // do something with the parameters...
    // be careful, this can easily result in a ArrayIndexOutOfBounds exception
    // if you try to access more parameters than you handed over

    long someLong;
    int someInt;

    // do something here with params
    // the params could for example contain an url and you could download stuff using this url here

    // the Integer variable is used for progress
    publishProgress(someInt);

    // once the data is downloaded (for example JSON data)
    // parse the data and return it to the onPostExecute() method
    // in this example the return data is simply a long value
    // this could also be a list of your custom-objects, ...
    return someLong;
 }

 // this is called whenever you call puhlishProgress(Integer), for example when updating a progressbar when downloading stuff
 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 // the onPostexecute method receives the return type of doInBackGround()
 protected void onPostExecute(Long result) {
     // do something with the result, for example display the received Data in a ListView
     // in this case, "result" would contain the "someLong" variable returned by doInBackground();
 }
}

=> How to use AsyncTask? How can I "call" it? How can I "execute" it?

In this case, the AsyncTask takes a String or String Array as a Parameter which will look like this once the AsyncTask is called: (The specified parameter is used in the execute(param) method of AsyncTask).

new DownloadFilesTask().execute("Somestring"); // some String as param

Be aware, that this call does not have a return value, the only return value you should use is the one returned from doInBackground(). Use the onPostExecute() method do make use of the returned value.

Also be careful with this line of code: (this execution will actually have a return value)

long myLong = new DownloadFilesTask().execute("somestring").get();

The .get() call causes the UI thread to be blocked (so the UI freezes if the operation takes longer than a few millisecons) while the AsyncTask is executing, because the execution does not take place in a separate thread. If you remove the call to .get() it will perform asynchronously.

=> What does this notation "execute(String... params)" mean?

This is a method with a so called "varargs" (variable arguments) parameter. To keep it simple, I will just say that it means that the actual number of values you can pass on to the method via this parameter is not specified, and any amount of values you hand to the method will be treated as an array inside the method. So this call could for example look like this:

execute("param1");

but it could however also look like this:

execute("param1", "param2");

or even more parameters. Assuming we are still talking about AsyncTask, the parameters can be accessed in this way in the doInBackground(String... params) method:

 protected Long doInBackground(String... params) {

     String str1 = params[0];
     String str2 = params[1]; // be careful here, you can easily get an ArrayOutOfBoundsException

     // do other stuff
 }

You can read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html

Also take a look at this AsyncTask example: https://stackoverflow.com/a/9671602/1590502

Share:
16,158
Kevin Van Ryckegem
Author by

Kevin Van Ryckegem

Software Architect / Team Lead at Realdolmen Software Architect (contractor) at Toyota Motor Europe

Updated on June 04, 2022

Comments

  • Kevin Van Ryckegem
    Kevin Van Ryckegem almost 2 years

    AsyncTask question

    I've followed some tutorials but it still isn't clear to me. Here's the code I currently have with some questions below the code. MainActivity calls SomeClassWithHTTPNeeds, which then calls the JSONParser (AsyncTask<>)


    MainActivity:

    String station = SomeClassWithHTTPNeeds.getInstance().getStation(123);
    

    SomeClassWithHTTPNeeds:

    getStation {
    
    JSONParser = new JSONParser();
    JSONObject station = parser.getJSONFromUrl("https://api....");
    return JSONObject.getString("station");
    }
    

    JSONParser (AsyncTask< String, Void, String >)

    protected String doInBackground(); ==> Seperate thread
    protected void onPostExecute(); ==> On GUI thread
    

    I was thinking: --- Put the HTTPRequest in doInBackground();

    Problem is I'm not sure how to: get the JSONParser to return the JSONObject to the getStation method?

    What I need to know

    => Where should I return the JSONObject: in background or execute?

    => How do I use the JSONParser once it's an AsyncTask? Will the execute() function return the value?

    => AsyncTask< String, Void, String > ==> How does this work? It's the return type?

    Thanks a lot!

  • Kevin Van Ryckegem
    Kevin Van Ryckegem over 10 years
    Thanks for this answer! Exactly what I was looking for, detailed and brilliant! I've done what you said, and in order to catch the return value in the class executing the task, I've used the .execute().get(); method. It seems to be working great.
  • Philipp Jahoda
    Philipp Jahoda over 10 years
    Allright, you are welcome. Nevertheless, be very careful with .get() -> see my edited answer.
  • Kevin Van Ryckegem
    Kevin Van Ryckegem over 10 years
    Yeah, I've noticed it, thanks for telling. In my case it's not a problem. However I'm not sure how it's possible to get the return value in the class executing the AsyncTask (in another way than .get())? What I need to execute can't be done in the onPostExecute() because I don't have access to the right variables. And I'd need to pass multiple types of variables to the AsyncTask to get them there. Otherwise I could define them as "Object" and then cast them to the right type? But it doesn't seem so proper :).
  • Philipp Jahoda
    Philipp Jahoda over 10 years
    I assume that you are talking about variables that are inside your Activity? Simply make the AsyncTask an inner class of your activity. In that way you will have access to all the variables :)
  • Kevin Van Ryckegem
    Kevin Van Ryckegem over 10 years
    I've made quite a few AsyncTasks now (without the .get()) and everything is running very fast and smoothly! This has definitely helped me.
  • zapl
    zapl about 10 years
    get() is as bad as disabling NetworkOnMainThreadException when doing it without a thread since it is essentially the same result. You should IMO change "be careful" to never use.