得到异步任务的结果 [英] Get the result from async task

查看:167
本文介绍了得到异步任务的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个异步任务得到我的结果。如果我使用task.execute.get,我的用户界面将被冻结。我希望我的异步任务将独立的类,所以我不想把我的结果处理code在onPostExecute。我发现有关回拨数据从这里异步任务的一些信息:的http:// blog.evoxmusic.fr/content/how-implement-callback-asynctask 结果
在这里:的Andr​​oid的AsyncTask发送回调UI 结果
但问题是:
1,我不知道什么时候处理结果?
2,为什么要使用接口?
3,什么是使用一个接口与简单地把结果在公共领域的异步任务从onPostExecute的区别在哪里?

这是我的异步类:

 公共类AsyncCallWs延伸的AsyncTask<弦乐,太虚,字符串> {    私人ProgressDialog对话框;
    公共字符串方法名=;
    私人WebService的WS;
    私人的ArrayList< ServiceParam> paramsList;
    私人布尔hasParams;    公共AsyncCallWs(活动活动,字符串方法名){
        xLog.position();
        尝试{
            this.dialog =新ProgressDialog(活动);
            this.methodName =方法名;
            hasParams = FALSE;
        }赶上(例外五){
            xLog.error(e.getMessage());
        }
    }    公共AsyncCallWs(活动活动,字符串methodName中,ArrayList的< ServiceParam> PARAMS){
        xLog.position();
        尝试{
            this.dialog =新ProgressDialog(活动);
            this.methodName =方法名;
            this.paramsList =参数;
            hasParams = TRUE;
        }赶上(例外五){
            xLog.error(e.getMessage());
        }
    }
    @覆盖
    在preExecute保护无效(){
        this.dialog.setMessage(PersianReshape.reshape(载入中...));
        this.dialog.show();
    }    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        xLog.position();
        字符串结果=没有异步任务的结果!
        尝试{
            WS =新的WebService(PublicVariable.NAMESPACE,PublicVariable.URL);
            如果(!hasParams){
                结果= ws.CallMethod(方法名);
            }
            其他{
                xLog.info(此方法是:+方法名);
                结果= ws.CallMethod(方法名,paramsList);
                xLog.info(这个结果是:+结果);
            }
        }赶上(例外五){
            xLog.error(e.getMessage());
        }
        返回结果;
    }    @覆盖
    保护无效onPostExecute(字符串结果){
        xLog.position();        如果(this.dialog.isShowing()){
            this.dialog.dismiss();
        }
        xLog.info(当前的AsyncTask的输出是:+结果);
    }
}


解决方案

  

1,我不知道什么时候处理结果?


结果将在 onPostExecute ,这反过来将调用任何类实现此接口的接口方法进行处理。所以实际UI的东西都会拿在活动地方片段或无论是实现该接口的回调。你可以你想要的任何数据传递给它。


  

2,为什么要使用接口?


一个接口是从分离逻辑的好方法你的的AsyncTask 和任何类(我假设一个活动片段),而实现它。这也意味着,实现此接口可以处理这一结果的任何类的AsyncTask ,它变得易于重复使用的。


  

3,什么是使用一个接口与简单地把结果在公共领域的异步任务从onPostExecute的区别在哪里?


您仍然不会得到一个回调 - 如何将你的活动片段知道什么时候该字段填充并准备审问?

I want to get my result from an Async task. If I use task.execute.get, my UI will be frozen. I want my Async task will be stand alone class so I don't want to put my result processing code in onPostExecute. I've found some information about call back data from Async task here: http://blog.evoxmusic.fr/content/how-implement-callback-asynctask
and here: android asynctask sending callbacks to ui
but the problem is: 1-I don't know when to process the result? 2-why to use interface? 3-What's the differences of using an interface with simply putting the result in a public field in Async task from onPostExecute?

This is my Async class:

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

解决方案

1-I don't know when to process the result?

The result will be processed in onPostExecute, which in turn will call your interface method in whatever class is implementing this interface. So the actual UI stuff will all take place in your Activity or Fragment or whatever is implementing the interface callback. You can pass any data you want to it.

2-why to use interface?

An interface is a great way to decouple the logic from your AsyncTask and whatever class (I assume an Activity or Fragment) that is implementing it. Also this means that any class that implements this interface can process results from this AsyncTask, it become easily re-usable.

3-What's the differences of using an interface with simply putting the result in a public field in Async task from onPostExecute?

You still won't get a callback - how will your Activity or Fragment know when this field is populated and ready to be interrogated?

这篇关于得到异步任务的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆