Search code examples
javaandroidweb-servicesandroid-asynctaskksoap2

How can i use two web service methods in the same activity?


I have two methods which are from soap webservice. I am calling them in asyntask that is a superclass of info.java page and tring to get the results in onPost method of asyntask. The calling code of info.java/onCreate is below.

        try{
        PropertyInfo propertyInfo1 = new PropertyInfo();
        properties.clear();

        propertyInfo1 = new PropertyInfo();
        propertyInfo1.setName("Module_id");
        propertyInfo1.setType(String.class);
        propertyInfo1.setValue(Utils.selectedModule_id);
        properties.add(propertyInfo1);

        new Info.AsyncTaskService().execute(new ServiceParams("GetInfo", properties), new ServiceParams("GetInfo_Photo", properties));

    } catch (Exception e) {
        Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG);
    }

Both of the service methods takes the same properties thats why i gave them same properties. My problem is i can't take the results because i know that it needs to call these two methods in different threads with an order but i don't know how to do it. Could you help me please? The codes of asynctask class is also below thank you.

 public class AsyncTaskService extends AsyncTask<ServiceParams, Void, Void> {
    String resp = "";
    String resp2 = "";
    ProgressDialog progressDialog;

    @Override
    protected Void doInBackground(ServiceParams... params) {
        resp = WebService.invoke(params[0].properties, params[0].methodName);
        resp2 = WebService.invoke(params[1].properties, params[1].methodName);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        Log.w("WEBSERVICE RESPONSE===", resp);

        Log.w("WEBSERVICE RESPONSE===", resp2);
        try {
            JSONArray ja = new JSONArray(resp);
            Utils.subMenuArrayList.clear();
            Info_Item info_item=new Info_Item(ja.getJSONObject(0));
            ((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo());
            ((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (progressDialog != null)
            progressDialog.dismiss();
    }
    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(Info.this);
        if (progressDialog != null) {
            progressDialog.setCancelable(false);
            progressDialog.setMessage("İşlem yapılıyor ...");
            progressDialog.show();
        }
    }

    protected void onProgressUpdate(Integer... progress) {
        if (progressDialog != null)
            progressDialog.setProgress(progress[0]);
    }

}

Solution

  • I have found how to do it! Wanted to share with you too.

    First of all describe you async tasks as below. I have two methods which i want to use in one activity at the same time(paralel) so i described two async task classes.

    public class FirstAsyncTask extends AsyncTask<ServiceParams, Void, Void> {
        String resp = "";
        ProgressDialog progressDialog;
    
        @Override
        protected Void doInBackground(ServiceParams... params) {
            resp = WebService.invoke(params[0].properties, params[0].methodName);
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
    
            Log.w("WEBSERVICE RESPONSE===", resp);
    
            try {
                JSONArray ja = new JSONArray(resp);
                Utils.subMenuArrayList.clear();
                Info_Item info_item=new Info_Item(ja.getJSONObject(0));
                ((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo());
                ((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (progressDialog != null)
                progressDialog.dismiss();
        }
    
        @Override
        protected void onPreExecute() {
    
            progressDialog = new ProgressDialog(Info.this);
            if (progressDialog != null) {
                progressDialog.setCancelable(false);
                progressDialog.setMessage("İşlem yapılıyor ...");
                progressDialog.show();
            }
        }
    
        protected void onProgressUpdate(Integer... progress) {
            if (progressDialog != null)
                progressDialog.setProgress(progress[0]);
        }
    
    }
    

    Then you should call the tasks like this with executeOnExecuter in your activity's onCreate method. I used a property array here to hold the parameters i am going to send to web service method and describe a serviceparameter with properties and method name and send them in executeOnExecuter() method. I used same properties for my both web service methods but you can describe an other property array like this "private ArrayList properties = new ArrayList<>();" and add informations you need for parameters you will send to web service method.

    try{
            PropertyInfo propertyInfo1 = new PropertyInfo();
            properties.clear();
    
            propertyInfo1 = new PropertyInfo();
            propertyInfo1.setName("Module_id");
            propertyInfo1.setType(String.class);
            propertyInfo1.setValue(Utils.selectedModule_id);
            properties.add(propertyInfo1);
    
            ServiceParams serviceparams=new ServiceParams("GetInfo", properties);
            ServiceParams serviceparams2=new ServiceParams("GetInfo_Photo", properties);
    
            FirstAsyncTask asyncTask = new FirstAsyncTask(); // First
            asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams);
            SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second
            asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams2);
    
        } catch (Exception e) {
            Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG);
        }