0

I created a class DataAdapter which launches threads to get data from webservices. When called in an activity using

DataAdapter.InitData();

how can I know when both threads are finished?

Thanks Jul

public class DataAdapter {

    private final static String URL = "http://www.mywebservice.com";
    private final static String URL_AD = "http://www.mywebservice2.com";

    public void InitData(){

       new GetInitData().execute(URL);
       new GetAd().execute(URL_AD);

    }

    private static class GetInitData extends AsyncTask<String, Integer, JSONObject> {
        protected JSONObject doInBackground(String... urls) {  

            JSONObject json = RestJsonClient.getJSONObject(urls[0]);
            return json;
        }

        protected void onProgressUpdate(Integer... progress) {                
        }

        protected void onPostExecute(JSONObject json) {          

           //process data
        }
    }

    private static class GetAd extends AsyncTask<String, Integer, JSONObject> {
        protected JSONObject doInBackground(String... urls) {  

            JSONObject json = RestJsonClient.getJSONObject(urls[0]);
            return json;
        }

        protected void onProgressUpdate(Integer... progress) {                
        }

        protected void onPostExecute(JSONObject json) {          

           //process data
        }
    }
}
jul
  • 36,404
  • 64
  • 191
  • 318
  • In your onPostExecute in each class call a method to set a boolean in another thread that is waiting for both to finish. When they have both finished you can call another method to do whatever you want. – Blundell May 24 '11 at 12:14

2 Answers2

0

Use this:

public class DataAdapter {

private final String URL = "http://www.mywebservice.com";
private final String URL_AD = "http://www.mywebservice2.com";
private boolean finished = false;

public void InitData(){

   new GetInitData(this).execute(URL);
   new GetAd(this).execute(URL_AD);

}

public void finished(){  
  if(!finished){
   finished = true;
  }else{
   Log.d("TAG","Both have finished"):
  }


private class GetInitData extends AsyncTask<String, Integer, JSONObject> {
    Activity ac = null;
    public GetInitData(Activity ac){
      this.ac=ac;
    }

    protected JSONObject doInBackground(String... urls) { 

        JSONObject json = RestJsonClient.getJSONObject(urls[0]);
        return json;
    }

    protected void onProgressUpdate(Integer... progress) {                
    }

    protected void onPostExecute(JSONObject json) {          

       //process data
       ac.finished();
    }
}

private class GetAd extends AsyncTask<String, Integer, JSONObject> {
   Activity ac = null;
    public GetInitData(Activity ac){
      this.ac=ac;
    }

    protected JSONObject doInBackground(String... urls) {  

        JSONObject json = RestJsonClient.getJSONObject(urls[0]);
        return json;
    }

    protected void onProgressUpdate(Integer... progress) {                
    }

    protected void onPostExecute(JSONObject json) {          

       //process data
       ac.finished();
    }
}

}

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • What's `Activity ac = null; public GetInitData(Activity ac){ this.ac=ac; }` for? – jul May 24 '11 at 14:38
  • To get a reference to the activity that you are calling. You can have AsyncTasks as private classes, but that is not a very good policy. When using them as public classes you will need that. Read this: http://stackoverflow.com/questions/4823891/android-asynctask-recommendations-private-class-or-public-class – neteinstein May 24 '11 at 16:47
0

Add a synchronized method to you DataAdapter class which each of the AsyncTask calles in its onPostExecute(). Within this method you set a boolean variable indicating that the first job finished, when the second AsyncTask calls the method it checks whether the first thread has already finished. If both have finished you can go one with your custom code.

Flo
  • 27,355
  • 15
  • 87
  • 125