Multiple and repeating AsyncTask in Android

I have two asyncases that work with each other. I use them to create a restaurant menu. The first web service retrieves the menu names from the database. The second web service retrieves the header elements from the database. I get header data in my first asintet and element data in my second asintet.

For example, I have ten menu names. There are eight items for each title. I perform the first synthesis and get all the menu names. I want to call a second asynthesis in the first asynctask onPostExecute to get this header element and add a TextView. I have to wait for the completion of every second task to add an item accordingly.

In short, I need to call AsyncTask first and wait. Then send a request for a second AsyncTask to First AsyncTask. I have to wait until every request is over. How can i wait

Here is my code.

First 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(); } } } 

my function which is used in the first 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; } 

Second AsyncTask

 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); } } } } 

my function which is used in the second 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; } 
0
source share
1 answer

If you want to wait for all AsyncTasks to complete before continuing, why don't you just put all of you on doInBackground from the first AsyncTask?

Or do you not want to do this because you want to run 10-second tasks in parallel? (Which, by the way, you don’t do anyway, because you are not using the ENREAD_POOL Executor for your tasks.) If so, then why not just do something like

 // 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(); } } 
+2
source

All Articles