【问题标题】:Android return value from processFinish in AsyncTask [duplicate]Android从AsyncTask中的processFinish返回值[重复]
【发布时间】:2014-09-10 08:46:18
【问题描述】:

在下面的代码中,我想从processFinish 方法传递数据以在外部类中使用该数据,但我无法解决问题,并且我无法在call 函数中从processFinish 返回值。

public class WSDLHelper  implements AsyncResponse{
    public SoapObject request;

    private String MainResult;

    public String call(SoapObject rq){

        new ProcessTask(rq, this).execute();
        return MainResult;
    }

    @Override
    public void processFinish(String output) {
        MainResult = output;
    }
}

class ProcessTask extends AsyncTask<Void, Void,  String> {
    public AsyncResponse delegate=null;

    SoapObject req1;

    public ProcessTask(SoapObject rq, AsyncResponse delegate) {
        req1 = rq;
        this.delegate = delegate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... params) {

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(this.req1);

        AndroidHttpTransport transport = new AndroidHttpTransport(Strings.URL_TSMS);
        transport.debug = true;

        String result = null;
        try {
            transport.call(Strings.URL_TSMS + this.req1.getName(), envelope);
            result = envelope.getResponse().toString();
        } catch (IOException ex) {
            Log.e("" , ex.getMessage());
        } catch (XmlPullParserException ex) {
            Log.e("" , ex.getMessage());
        }

        if (result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE))) {
            try {
                throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
            } catch (TException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    protected void onPostExecute(String result) {
        delegate.processFinish(result);
    }
}

请帮助我开发call 方法以将output 值返回给外部类。谢谢

【问题讨论】:

  • onPostExecute 上的委托是否为空?因为如果不是,那么它会按预期工作。
  • @PedroOliveira 是的,那是空的。如何解决?
  • Same exact question answered here。 Mahdi,请理解 return MainResult; MainResult = output; 之前运行,因此 call(...) 将始终返回 null,无论如何你做什么。
  • 这两个问题实际上都是this one的转贴。

标签: android android-asynctask


【解决方案1】:

ProcessTask 是一个异步任务,所以当你调用callmethod 时,你会在return MainResult 时得到null,因为它还没有被processFinishmethod 设置。

【讨论】:

  • 发布更新。谢谢
【解决方案2】:

您正在实施 AsyncResponce。 所以 替换

new ProcessTask(rq, new AsyncResponse() {
            @Override
            public void processFinish(String output) {
                MainResult = output;
            }
        }).execute();

new ProcessTask(rq, this).execute();

然后你的类overrided方法processFinish(String output)会调用。

并在 processFinish() 中使用您的值,而不是 call() 方法返回的值。

【讨论】:

  • 使用此实现的方法@Override public void processFinish(String output) { MainResult = output; } 无法解决new ProcessTask(rq, this).execute();return MainResult; 的问题
  • 你调试过你的代码有什么异常吗?
  • 是的。那就是返回null
  • 您正在从 call(...) 方法返回值。请在 processFinished() 上使用值。
  • 这是 AsyncTask 功能,不能用 call 方法返回。
猜你喜欢
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2012-02-13
相关资源
最近更新 更多