AsyncTask.get() blocks the flow of the caller. Use AsyncTask.execute() .
public final Result get ()
Added to API Level 3
Waits if necessary to complete the calculation, and then retrieves its result.
Returns the calculated result.
Drawing from
How to return boolean value from AsyncTask?
Try below
new DownloadJSONTask(ActivityName.this).execute(url);
In your DownloadJSONTask
At construcotr
TheInterface listener; public DownloadJSONTask(Context context) { listener = (TheInterface) context; }
Interface
public interface TheInterface { public void theMethod(ArrayList<String> result);
In your doInbackground return the result. I assume its an ArrayList of type String. Change arraylist to suit your requirements.
In your onPostExecute
if (listener != null) { listener.theMethod(result); // result is the ArrayList<String> // result returned in doInbackground // result of doInbackground computation is a parameter to onPostExecute }
In your activity class implement an interface
public class ActivityName implements DownloadJSONTask.TheInterface
Then
@Override public void theMethod(ArrayList<String> result) {
Edit: Alternative
You can make your asintesque an inner class of your activity class. The result of evaluating doInbackground is the onPostExecute parameter. Return result doInbackground . Update ui in onPostExecute .
source share