Reputation: 177
I am developing an android application which uses Jsoup to retrieve web content. To execute this, I am using separate AsyncTask
classes. I need to show the retrieved results after the finish of AsyncTask
s. The doInBackground
method stores retrieved data into an Array.
ScheduleExtractor scheduleExtractor = new ScheduleExtractor();
scheduleExtractor.execute();
if(myArray.length==0){
///
}else{
//show results
}
However, when I try to access the array after calling the method Asynctask.execute()
, the array has no elements. So, is there a way to make sure that the access to the array happens after finishing the AsyncTask
method? Thank you.
Upvotes: 0
Views: 40
Reputation: 5598
AsyncTask
s as the name represents do you job asynchronous, meaning that the call to execute()
starts the process in a different thread while the rest of your code keeps on running on your own thread right after calling execute()
.
If you expect your AsyncTask
to have a final result you should override onPostExecute(Result)
method and check the length of your array there. Here is a simple example (assuming the final result type is int[]
:
class ScheduleExtractor extends AsyncTask<Void, Void, int[]> {
@Override
protected int[] doInBackground(Void... params) {
// Process your retrieved data and return the result here
}
@Override
protected void onPostExecute(int[] arrayResult) {
if(arrayResult.length==0) {
// Your code here
} else {
// Show results
}
}
}
Upvotes: 1