显示加载UI冻结异步任务,如此

时间:2013-12-10 05:34:25

标签: android asynchronous android-asynctask loading webmethod

我有一个Async任务类,它获取Web方法的名称并运行它,我必须等待该Web方法的结果,所以我使用了task.execute.get()方法来冻结我的UI。问题是我想在任务执行时显示加载对话框,但是当我试图为10个Web方法调用此方法10次时,UI冻结并在执行10个Web方法后,加载对话框显示1秒钟。<登记/> 如果不将所有代码移动到doInBackground中,我可以做什么来显示加载?我想要一个获取Web方法信息并返回结果的类。这是我的班级代码:

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

1 个答案:

答案 0 :(得分:1)

这里

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

与文档AsyncTask.get()中一样:

  

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

所以为了避免在执行doInBackground start AsyncTask期间冻结主UI线程而不调用get方法为|:

t.execute();
  

我希望有一个获取Web方法信息的类并返回   结果

为此,您应该使用AsyncTask实现回调,并向Activity报告。请参阅以下示例:

android asynctask sending callbacks to ui

How to implement callback with AsyncTask