0

我有两个 asynctask 互相工作。我正在使用它们来创建餐厅菜单。第一个 Web 服务从数据库中获取菜单的标题。第二个 Web 服务从数据库中获取标题项目。我在第一个异步任务中获取标题数据,在第二个异步任务中获取项目数据。

例如,我有十个菜单标题。每个标题有八个项目。我执行第一个 asynctask 并获取所有菜单标题。我想在第一个 asynctask 的 onPostExecute 中调用第二个 asynctask 以获取此标题的项目并添加 TextView。我必须等待完成每个第二个任务才能分别添加项目。

简而言之,我需要先调用 AsyncTask 并等待完成它。然后向 First AsyncTask 中的第二个 AsyncTask 发送请求。我必须等待每个请求完成。我怎么能等?

这是我的代码。

第一个异步任务

public class BaslikDoldurAS extends AsyncTask<String,String[][],String[][]>{
        int ParamID;

        public BaslikDoldurAS(String ParamID){
            this.ParamID=Integer.parseInt(ParamID);
        }

        @Override
        protected String[][] doInBackground(String... params) {
            BaslikDoldur(ParamID);
            return sonuc;
        }

        protected void onPostExecute(String[][] sonuc){
            for(int i=0;i<baslikCount;i++){ 
                 MenuDoldurAS kontrol = new MenuDoldurAS(firma_id,sonuc[2][i]);
                 kontrol.execute();
            } 
        }
    }

我在第一个 asyncTask 中使用的函数

private String[][] BaslikDoldur(Integer ParamID){
        PropertyInfo id = new PropertyInfo();
        id.name= "id";
        id.setValue(ParamID);
        id.type = PropertyInfo.INTEGER_CLASS;

        SoapObject request = new SoapObject(NAMESPACE, "BaslikDoldur");
        request.addProperty(id);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut=request;
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(MenuURL);
        androidHttpTransport.debug = true;

        try {

            androidHttpTransport.call("http://tempuri.org/BaslikDoldur", envelope);
            SoapObject response = (SoapObject) envelope.getResponse();

            sonuc[2]=new String[response.getPropertyCount()]; //baslik
            baslikCount=response.getPropertyCount();

            for(int i=0;i<response.getPropertyCount();i++){    
                   Object property = response.getProperty(i);
                   if(property instanceof SoapObject){
                       SoapObject menu = (SoapObject) property;
                       sonuc[2][i] = menu.getProperty("menu_baslik").toString();
                   }
            }
     } 
             catch (Exception e) {          
                e.printStackTrace();
            }
        return sonuc;

    }

第二个异步任务

public class MenuDoldurAS extends AsyncTask<String,String[][],String[][]>{
        int ParamID;
        String Baslik;

        public MenuDoldurAS(String ParamID,String Baslik){
            this.ParamID=Integer.parseInt(ParamID);
            this.Baslik=Baslik;
        }
        @Override
        protected String[][] doInBackground(String... params) {
            MenuDoldur(ParamID,Baslik);
            return sonuc;
        }

        protected void onPostExecute(String[][] sonuc){
            for(int i=0;i<count;i++){
                String baslik="";
                if(!baslik.equals(sonuc[2][i])){
                    baslik=sonuc[2][i];
                    TextView basliktxt = new TextView(Urun.this);
                    basliktxt.setText(sonuc[2][i]);
                    basliktxt.setTextSize(20);
                    basliktxt.setTextColor(Color.RED);
                    basliktxt.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    urunLayout.addView(basliktxt);
                }
                else{
                    TextView aciklamatxt = new TextView(Urun.this);
                    aciklamatxt.setText(sonuc[3][i]);
                    aciklamatxt.setTextColor(Color.parseColor("#0c0c7c"));
                    aciklamatxt.setTextSize(17);
                    aciklamatxt.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    urunLayout.addView(aciklamatxt);
                }
            }   
        }

    }

我在第二个 asyncTask 中使用的函数

private String[][] MenuDoldur(Integer ParamID,String Baslik){
        PropertyInfo id = new PropertyInfo();
        id.name= "id";
        id.setValue(ParamID);
        id.type = PropertyInfo.INTEGER_CLASS;

        PropertyInfo baslik = new PropertyInfo();
        baslik.name= "baslik";
        baslik.setValue(Baslik);
        baslik.type = PropertyInfo.STRING_CLASS;

        SoapObject request = new SoapObject(NAMESPACE, "MenuDoldur");
        request.addProperty(id);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut=request;
        envelope.dotNet = true;     
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(MenuURL);
        androidHttpTransport.debug = true;

        try {

            androidHttpTransport.call("http://tempuri.org/MenuDoldur", envelope);
            SoapObject response = (SoapObject) envelope.getResponse();

            sonuc[3]=new String[response.getPropertyCount()]; //aciklama ve fiyat
            count = response.getPropertyCount();

            for(int i=0;i<response.getPropertyCount();i++){    
                   Object property = response.getProperty(i);
                   if(property instanceof SoapObject){
                       SoapObject menu = (SoapObject) property;
                       sonuc[3][i] = menu.getProperty("menu_aciklama").toString() + " - " + menu.getProperty("menu_fiyat").toString();
                   }
            }
     } 
             catch (Exception e) {          
                e.printStackTrace();
            }
        return sonuc;   

    }
4

1 回答 1

2

如果你想等到所有 AsyncTask 都完成后再继续,为什么不把所有的工作都放在第一个 AsyncTask 的 doInBackground 中呢?

或者您不想这样做,因为您想并行运行 10 个“第二个任务”?(顺便说一句,你什么也没做,因为你没有使用 THREAD_POOL Executor 来执行你的任务。)如果是这种情况,那么为什么不做类似的事情

// variable accessible to both tasks
ArrayList<AsyncTask> mRunningTasks = new ArrayList<AsyncTask>();
// AsyncTask1
protected void onPostExecute(String[][] sonuc){
    for(int i=0;i<baslikCount;i++){ 
         MenuDoldurAS kontrol = new MenuDoldurAS(firma_id,sonuc[2][i]);
         mRunningTasks.add(kontrol);
    }
    for (AsyncTask task : mRunningTasks) {
        task.execute();
    }
}

// AsyncTask2
protected void onPostExecute(...) {
    boolean allComplete = true;
    for (AsyncTask task : mRunningTasks) {
      if (!task.getStatus().equals(AsyncTask.Status.FINISHED)) {
          allComplete = false;
          break;
      }
    }
    if (allComplete) {
       //do whatever
       mRunningTasks.clear();
    }
}
于 2012-08-10T22:33:20.337 回答