2013-12-10 40 views
0

我有一个异步任务类,它获取Web方法的名称并运行它,我必须等待该Web方法的结果,所以我使用task.execute .get()方法冻结了我的UI。问题是我想在任务执行时显示一个加载对话框,但是当我试图为10个Web方法调用此方法10次时,UI冻结并且在执行10个Web方法后,加载对话框出现1秒。
我可以做什么来显示加载而无需将所有代码移入doInBackground?我想要一个获取Web方法信息并返回结果的类。这是我的类代码:显示加载的UI冻结异步任务像这样

public class AsyncCallWs extends AsyncTask<String, Void, String> { 

    private ProgressDialog dialog; 
    public String methodName=""; 
    private WebService ws; 
    private ArrayList<ServiceParam> 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<ServiceParam> 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); 
    } 
} 

这是我打电话使用这个类Web方法的方式:

public void doSync(String method){ 
     xLog.position(); 
     AsyncCallWs t; 
     ArrayList<ServiceParam> serviceParams = new ArrayList<ServiceParam>(); 
     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=>"+ 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=>"+ 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<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=>"+ result); 
       if (!result.equals("Nothing")){ 
        String records[] = result.split(PublicVariable.RECORD_SPLITTER); 
        News news = new News(activity); 
        news.empty(); 
        for(int i=0;i<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=>"+ 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<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=>"+ 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<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=>"+ 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<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=>"+ 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<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()); 
      } 
     } 
    } 

回答

0

这里

result = t.execute().get(); //<<< calling get method 

在DOC AsyncTask.get()

如果需要,等待计算到c完成,然后检索其结果 。

所以要避免主UI线程的冻结doInBackground开始AsyncTask的执行过程中没有调用get方法|:

t.execute(); 

我想有一类获取Web方法的信息,并返回 结果

为此,您应该使用AsyncTask实现回调,该回调报告给Activity。看下面的例子:

android asynctask sending callbacks to ui

How to implement callback with AsyncTask