Saturday, 27 May 2017

java - How to use AsyncTask



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!


Answer



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



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


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 {

// 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


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...