【问题标题】:Showing loading for a UI freezing async task like this one显示像这样的 UI 冻结异步任务的加载
【发布时间】:2013-12-10 05:34:24
【问题描述】:

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

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

【问题讨论】:

    标签: android asynchronous android-asynctask loading webmethod


    【解决方案1】:

    这里

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

    如文档AsyncTask.get():

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

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

    t.execute();
    

    我想要一个类来获取网络方法信息并返回 结果

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

    android asynctask sending callbacks to ui

    How to implement callback with AsyncTask

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2020-05-10
      • 1970-01-01
      • 2015-09-11
      相关资源
      最近更新 更多