如何返回从ASYN调用结果 [英] how to return result from asyn call

查看:152
本文介绍了如何返回从ASYN调用结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的班级我所有的异步调用,所以我不想有全局变量被设置aync'ly。要做到这一点,我想返回对象如:从我的后处理asunc方法的字符串。

可以这样做?

下面是我的总体结构,以我的班,我想从onPostExecute返回一个字符串,例如()。我看到代表们在其他地方提到但这似乎很凌乱,肯定有办法有一个返回类型的类或方法?

 类GetStuffAsyncly扩展的AsyncTask<字符串,字符串,字符串>
{
    //我瓦尔....    公共myconstructor(字符串dialogMessage,上下文CON)
    {
        this.qDialog =新ProgressDialog(CON);
        this.dialogString = dialogMessage;
        this.context = CON;
    }    / **
     *启动后台线程显示进度对话框之前
     * * /
    @覆盖
    在preExecute保护无效()
    {
        super.on preExecute();
        做的东西像火对话框
    }    @覆盖
    保护字符串doInBackground(字符串参数... args)
    {
       //做的东西在后台...        返回的数据;
    }    / **
     *在完成后台任务之后辞退进度对话框
     * ** /
    保护无效onPostExecute(字符串jsonString)
    {
        //关闭该对话框获取所有数据后,
        dialog.dismiss();
    }
}


解决方案

有些东西像下面

 类GetStuffAsyncly扩展的AsyncTask<字符串,字符串,字符串> {
    串dialogString;
    ProgressDialog对话框;
    上下文语境;
    AsyncListener监听;
    //我瓦尔....    公共GetStuffAsyncly(字符串dialogMessage,上下文CON,AsyncListener听众){
        this.dialog =新ProgressDialog(CON);
        this.dialogString = dialogMessage;
        this.context = CON;
        this.listener =侦听器;
    }    / **
     *启动后台线程显示进度对话框之前
     * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        listener.onTaskStarted();
    }    @覆盖
    保护字符串doInBackground(字符串参数... args){
        //做的东西在后台...        返回的数据;
    }    / **
     *在完成后台任务之后辞退进度对话框
     ** /
    保护无效onPostExecute(字符串jsonString){
        //关闭该对话框获取所有数据后,
        dialog.dismiss();
        listener.onTaskFinished(jsonString);
    }
}

和监听器类

 公共接口AsyncListener {
    无效onTaskStarted();    无效onTaskFinished(字符串数据);
}

和你可以调用像这样

 新GetStuffAsyncly(消息,对此,新AsyncListener(){
            @覆盖
            公共无效onTaskStarted(){
                //做你的东西
            }            @覆盖
            公共无效onTaskFinished(字符串数据){
//做你的东西;
            }
        })执行(参数);

i have all my async calls in their own classes and so i dont want to have global vars being set aync'ly. To do this i want to return objects eg a string from my asunc postProcess methods.

can this be done?

Below is my general structure to my classes, i want to return a String for example from onPostExecute(). I see delegates are mentioned in other places but this seems very messy, sure there is a way to have a return type to the class or methods?

class GetStuffAsyncly extends AsyncTask<String, String, String>
{
    // my vars....

    public myconstructor(String dialogMessage, Context con)
    {
        this.qDialog =  new ProgressDialog(con);
        this.dialogString = dialogMessage;
        this.context = con;
    }

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        do stuff like fire dialog
    }

    @Override
    protected String doInBackground(String... args)
    {
       // do stuff in background...

        return data;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String jsonString)
    {
        // dismiss the dialog after getting all data
        dialog.dismiss();
    }
}

解决方案

Some thing like below

class GetStuffAsyncly extends AsyncTask<String, String, String> {
    String dialogString;
    ProgressDialog dialog;
    Context context;
    AsyncListener listener;
    // my vars....

    public GetStuffAsyncly(String dialogMessage, Context con, AsyncListener listener) {
        this.dialog = new ProgressDialog(con);
        this.dialogString = dialogMessage;
        this.context = con;
        this.listener = listener;
    }

    /**
     * Before starting background thread Show Progress Dialog
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        listener.onTaskStarted();
    }

    @Override
    protected String doInBackground(String... args) {
        // do stuff in background...

        return data;
    }

    /**
     * After completing background task Dismiss the progress dialog
     **/
    protected void onPostExecute(String jsonString) {
        // dismiss the dialog after getting all data
        dialog.dismiss();
        listener.onTaskFinished(jsonString);
    }
}

And the listener class

public interface AsyncListener {
    void onTaskStarted();

    void onTaskFinished(String data);
}

and you can call like this

new GetStuffAsyncly(message, this, new AsyncListener() {
            @Override
            public void onTaskStarted() {
                //do your stuff
            }

            @Override
            public void onTaskFinished(String data) {
//Do your stuff;
            }
        }).execute(parameter);

这篇关于如何返回从ASYN调用结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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