android中如何从异步任务返回值

android中如何从异步任务返回值,android,android-asynctask,Android,Android Asynctask,我创建了一个异步任务来调用我的服务器从数据库获取数据。 我需要处理http服务器调用返回的结果。 在我的活动中,我在许多地方调用了异步任务。所以我不能使用成员变量来访问结果。有什么办法吗 public Result CallServer(String params) { try { new MainAynscTask().execute(params); } catch(Exception ex) { ex.printStackT

我创建了一个异步任务来调用我的服务器从数据库获取数据。
我需要处理http服务器调用返回的结果。
在我的活动中,我在许多地方调用了异步任务。所以我不能使用成员变量来访问结果。有什么办法吗

public Result CallServer(String params)
{

    try
    {
    new MainAynscTask().execute(params);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return aResultM;//Need to get back the result

}  

    private class MainAynscTask extends AsyncTask<String, Void, Result> {


    @Override
    protected Result doInBackground(String... ParamsP) {    
        //calling server codes
        return aResultL;
    }       
    @Override
       protected void onPostExecute(Result result) {
          super.onPostExecute(result);
          //how i will pass this result where i called this task?
       }
公共结果调用服务器(字符串参数)
{
尝试
{
新建mainayncstask().execute(参数);
}
捕获(例外情况除外)
{
例如printStackTrace();
}
return aResultM;//需要返回结果
}  
私有类MainAyncTask扩展了AsyncTask{
@凌驾
受保护的结果doInBackground(字符串…ParamsP){
//调用服务器代码
返回结果;
}       
@凌驾
受保护的void onPostExecute(结果){
super.onPostExecute(结果);
//我将如何通过我称之为此任务的结果?
}

我可以建议两种方法-

  • AsyncTask
    中的
    onPostExecute(结果)
    。请参阅

  • 发送一个带有结果的广播作为额外信息


  • AsyncTask
    是一个异步任务,因此将结果返回给调用方没有意义。而是在
    onPostExecute()中处理结果
    如将值设置为
    TextView
    等,或发送广播,以便其他侦听器可以处理结果。

    执行异步任务时,任务将经历4个步骤:

  • onPreExecute(),在执行任务之前在UI线程上调用。此步骤通常用于设置任务,例如在用户界面中显示进度条

  • doInBackground(Params…),在onPreExecute()之后立即在后台线程上调用完成执行。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数将传递到此步骤。计算结果必须在此步骤中返回,并将返回到最后一步。此步骤还可以使用publishProgress(Progress…)发布一个或多个进度单位。这些值在UI线程的onProgressUpdate(进度…)步骤中发布

  • onProgressUpdate(Progress…),在调用publishProgress(Progress…)后在UI线程上调用。执行时间未定义。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于设置进度条动画或在文本字段中显示日志

  • onPostExecute(Result),在后台计算完成后在UI线程上调用。后台计算的结果作为参数传递到此步骤。

  • 使用处理程序

    在你的活动中

        Handler mHandler = new Handler() { 
        @Override public void handleMessage(Message msg) { 
            String s=(String)msg.obj;
            tv.setText(s);
        }
    };
    
    //result is soap object in this case.
    protected void onPostExecute(SoapObject result) {
        pd.dismiss();
        if(result != null) {
            Message msg=new Message();
            msg.obj=result.getProperty(0).toString();
            mHandler.sendMessage(msg);
        }
    

    以下是我如何处理这件事的:

    1) 创建一个接口类,该接口类定义完成时要执行的方法的签名:

    public interface AsyncIfc {
        public void onComplete();
    }
    
    2) 在AsyncTask类上设置一个属性以保存委托方法:

        public AsyncIfc completionCode;
    
    3) 从AsyncTask中的onPostExecute()触发委托:

    completionCode.onComplete();
    
    4) 从调用逻辑中,将委托属性设置为匿名方法:

    task.completionCode = new AsyncIfc() {
    
        @Override
        public void onComplete() {
        // Any logic you want to happen after execution
        }
    };
    
    在调用execute()方法之后,尝试调用AsyncTask的get()方法。这对我很有用


    为什么不调用一种方法来处理该值,如所示的一个方法,为什么不调用一个方法来处理该值,如所示的一个方法呢?可能的重复性和可能的重复性以及可能的重复性以及正确的方法是正确的方式是使用O、n、e、l、l、l、l、t、t、t、t、t、t、t、l、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、t、y、y、y、y、y、O、O、O、p、p、p、p、p、p、p、p、p、p、p、p、p、p、p、O、O、O、O、i、i、i、i、i、i、i、i、i、i、i、i、i、i、i、n、n、t、t、t、t、t、t、在本研究中,c̶c