title: Showing loading for a UI freezing async task like this one
body: <p>I have an Async task class which gets the name of a web method and run it and I have to wait for the result of that web method, so I used task.execute.get() method which is freezing my UI. the problem is that I want to show a loading dialog when task is executing but when I'm trying to call this method 10 times for 10 web methods, the UI freezes and after executing 10 web methods, loading dialog appears for 1 second.<br>
  What can I do to show loading without moving all of my codes into doInBackground? I want to have a class which gets web method info and returns the result. this is my class code:</p>
  
  <pre><code>public class AsyncCallWs extends AsyncTask&lt;String, Void, String&gt; {
  
      private ProgressDialog dialog;
      public String methodName="";
      private WebService ws;
      private ArrayList&lt;ServiceParam&gt; paramsList;
      private boolean hasParams; 
  
      public AsyncCallWs(Activity activity,String methodName) {
          xLog.position();
          try {
              this.dialog = new ProgressDialog(activity);
              this.methodName = methodName;
              hasParams = false;
          } catch (Exception e) {
              xLog.error(e.getMessage());
          }
      }
  
      public AsyncCallWs(Activity activity,String methodName,ArrayList&lt;ServiceParam&gt; params) {
          xLog.position();
          try {
              this.dialog = new ProgressDialog(activity);
              this.methodName = methodName;
              this.paramsList = params;
              hasParams = true;
          } catch (Exception e) {
              xLog.error(e.getMessage());
          }
      }
  
  
      @Override
      protected void onPreExecute() {
          this.dialog.setMessage(PersianReshape.reshape("Loading..."));
          this.dialog.show();
      }
  
      @Override
      protected String doInBackground(String... params) {
          xLog.position();
          String result = "No async task result!";
          try {
              ws = new WebService(PublicVariable.NAMESPACE, PublicVariable.URL);
              if (!hasParams){
                  result = ws.CallMethod(methodName);
              }
              else{
                  xLog.info("THIS METHOD IS: "+ methodName);
                  result = ws.CallMethod(methodName,paramsList);
                  xLog.info("THIS RESULT IS: "+ result);
              }
          } catch (Exception e) {
              xLog.error(e.getMessage());
          }
          return result;
      }
  
      @Override
      protected void onPostExecute(String result) {
          xLog.position();
  
          if (this.dialog.isShowing()) {
              this.dialog.dismiss();
          }
          xLog.info("Output of current AsyncTask is:"+ result);
      }
  }
  </code></pre>
  
  <p>And this is the way I'm calling web methods using this class:</p>
  
  <pre><code>public void doSync(String method){
          xLog.position();
          AsyncCallWs t;
          ArrayList&lt;ServiceParam&gt; serviceParams = new ArrayList&lt;ServiceParam&gt;();
          String result="";
  
          Settings settings = new Settings(activity);
          PublicVariable.pGuid = Login(settings.getValue("Username"), settings.getValue("Password"));  
  
          xLog.info("pGuid in doSync is:" + PublicVariable.pGuid);
          serviceParams.add(new ServiceParam("pGuid", PublicVariable.pGuid, String.class));
  
  
  
          if (method=="all" || method=="person"){
              try {
                  t = new AsyncCallWs(activity,"GetPersonInfo",serviceParams);
                  result = t.execute().get();
                  xLog.info("Sync Person=&gt;"+ result);
                  String fields[] = result.split(PublicVariable.FIELD_SPLITTER);
                  Person person = new Person(activity,fields);
                  person.empty();
                  person.insert();
                  settings.update("PersonId",String.valueOf(person.getId()));
                  PublicVariable.personId = person.getId();
                  xLog.info("Person inserted...");
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
  
          }
          if (method=="all" || method=="personImage"){
              try {
                  t = new AsyncCallWs(activity,"GetPersonImage",serviceParams);
                  result = t.execute().get();
                  if (!result.equals("Nothing")){
                      settings.update("picture", result);
                      xLog.info("Picture updatted...");
                  }
                  else
                      xLog.error("NO PERSON IMAGE FOUND!");
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
          if (method=="all" || method=="lawyers"){
              try {
                  t = new AsyncCallWs(activity,"GetLawyers",serviceParams);
                  result = t.execute().get();
                  xLog.info("Sync Lawyer=&gt;"+ result);
                  if (!result.equals("Nothing")){
                      String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                      String fields[];
                      Lawyer lawyer= new Lawyer(activity);
                      lawyer.empty();
                      for(int i=0;i&lt;records.length;i++){
                          fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                          lawyer = new Lawyer(activity, fields);
                          lawyer.insert();
                      }
                      xLog.info("Lawyers inserted...");
                  }
                  else
                      xLog.error("NO LAWYER FOUND!");
              }catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
          if (method=="all" || method=="news"){
              try {
                  t = new AsyncCallWs(activity,"GetNews",serviceParams);
                  result = t.execute().get();
                  String fields[];
                  Log.d("Ehsan","Sync News=&gt;"+ result);
                  if (!result.equals("Nothing")){
                      String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                      News news = new News(activity);
                      news.empty();
                      for(int i=0;i&lt;records.length;i++){
                          fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                          news= new News(activity,fields);
                          news.insert();
                      }
                      xLog.info("News inserted...");
                  }
                  else
                      xLog.error("NO NEWS FOUND!");
  
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
          if (method=="all" || method=="messages"){
              try {
                  t = new AsyncCallWs(activity,"GetMessagesInbox ",serviceParams);
                  result = t.execute().get();
                  Log.d("Ehsan","Sync message Inbox=&gt;"+ result);
                  if (!result.equals("Nothing")){
                      String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                      String fields[];
                      Message message = new Message(activity);
                      message.empty();
                      for(int i=0;i&lt;records.length;i++){
                          fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                          message= new Message(activity,fields);
                          message.insert();
                      }
                      xLog.info("Inbox messages inserted...");
                  }
                  else
                      xLog.error("NO MESSAGES FOUND!");
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
  
              try {
                  t = new AsyncCallWs(activity,"GetMessagesOutbox ",serviceParams);
                  result = t.execute().get();
                  Log.d("Ehsan","Sync message Outbox=&gt;"+ result);
                  if (!result.equals("Nothing")){
                  String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                  String fields[];
                  Message message = new Message(activity);
                  message.empty();
                  for(int i=0;i&lt;records.length;i++){
                      fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                      message= new Message(activity,fields);
                      message.insert();
  
                  }
                  xLog.info("Outbox messages inserted...");
                  }
                  else
                      xLog.error("NO MESSAGES FOUND!");
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
  
          }
          if (method=="all" || method=="requests"){
              try {
                  t = new AsyncCallWs(activity,"GetAllRequests",serviceParams);
                  result = t.execute().get();
                  Log.d("Ehsan","Sync share buy sell requests=&gt;"+ result);
                  if (!result.equals("Nothing")){
                  String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                  String fields[];
                  Share share = new Share(activity);
                  share.empty();
                  for(int i=0;i&lt;records.length;i++){
                      fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                      share= new Share(activity,fields);
                      share.insert();
                  }
                  xLog.info("Shares inserted...");
                  }
                  else
                      xLog.error("NO MESSAGES FOUND!");
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
          if (method=="all" || method=="financials"){
              try {
                  t = new AsyncCallWs(activity,"GetFinancials",serviceParams);
                  result = t.execute().get();
                  Log.d("Ehsan","Sync Financials=&gt;"+ result);
                  if (!result.equals("Nothing")){
                      String records[] = result.split(PublicVariable.RECORD_SPLITTER);
                      String fields[];
                      Financial financial = new Financial(activity);
                      financial.empty();
                      for(int i=0;i&lt;records.length;i++){
                          fields = records[i].split(PublicVariable.FIELD_SPLITTER);
                          financial= new Financial(activity,fields);
                          financial.insert();
                      }
                      xLog.info("Financials inserted...");
                  }
                  else{
                      Log.e("Ehsan", "NOT FINANCIALS FOUND!");
                  }
              } catch (Exception e) {
                  xLog.error(e.getMessage());
              }
          }
      }
  </code></pre>