2013-12-10 6 views
0

웹 메서드의 이름을 가져 와서 실행하는 비동기 작업 클래스가 있는데 그 웹 메서드의 결과를 기다려야하므로 task.execute .get() 메소드는 내 UI를 고정시킵니다. 문제는 작업을 실행할 때로드 대화 상자를 표시하려고하지만 10 개의 웹 메서드에 대해이 메서드를 10 번 호출하려고하면 UI가 멈추고 10 개의 웹 메서드를 실행 한 후로드 대화 상자가 1 초 동안 나타납니다.
모든 코드를 doInBackground로 이동하지 않고로드를 표시하려면 어떻게해야합니까? 웹 메서드 정보를 가져 와서 결과를 반환하는 클래스를 갖고 싶습니다. 이 내 수업 코드 :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); 
    } 
} 

그리고 이것은 내가이 클래스 사용하여 웹 메소드 호출 해요 방법 : 문서 AsyncTask.get()에서와 같이 여기

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

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

:

계산을 위해 필요하면 c를 기다립니다. omplete를 입력 한 다음 을 검색합니다.

그래서 get 메소드로 호출하지 않고 doInBackground 시작 AsyncTask의 실행시 메인 UI 스레드의 동결을 방지하기 위해 | :

t.execute(); 

내가 웹 메소드 정보를 가져오고을 반환하는 클래스를 갖고 싶어 결과

이 경우 Activity에보고하는 AsyncTask를 사용하여 콜백을 구현해야합니다. 다음 예를 참조하십시오

android asynctask sending callbacks to ui

How to implement callback with AsyncTask

관련 문제