java - Android 在 AsyncTask onPostExecute 中使用 Interface 返回值

标签 java android android-asynctask android-ksoap2

在下面的代码中,我想从 AsyncTask 返回值使用接口(interface)。但我得到了错误的值,并且无法从 onPostExecute 返回正确的值.

我开发了这个link教程与我的代码。我无法正确使用它。

界面:

public interface AsyncResponse {
    void processFinish(String output);
}

Ksoap主类:

public class WSDLHelper  implements AsyncResponse{
    public SoapObject request;

    private String Mainresult;

    public String call(SoapObject rq){

        ProcessTask p =new ProcessTask(rq);
        String tmp = String.valueOf(p.execute());

        p.delegate = this;

        return Mainresult;
    }


    @Override
    public void processFinish(String output) {

        this.Mainresult = output;
    }
}
class ProcessTask extends AsyncTask<Void, Void, Void > {
    public AsyncResponse delegate=null;

    SoapObject req1;
    private String result;
    public ProcessTask(SoapObject rq) {
        req1 = rq;
    }

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

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

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

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

        try {
            transport.call(Strings.URL_TSMS + this.req1.getName(), envelope);
            this.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();
            }

        }

        Log.e("------------++++++++++++++++-----------", this.result);

        return null;
    }

    protected void onPostExecute(String result) {
    /* super.onPostExecute(result);*/
        delegate.processFinish(this.result);
    }

}

请帮我解决这个问题

最佳答案

那是行不通的。您正在创建并执行 AsyncTask(异步!),然后在尚未收到结果时调用 return Mainresult(同步!)。解决办法是去掉多余的类WSDLHelper,直接访问ProcessTask

除此之外,您错误地使用了 AsyncTask(将结果保存在类变量中,而不是将其作为参数传递)。这是完整版本:

public 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 Void 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 != null && result.equals(String.valueOf(Integers.CODE_USER_PASS_FALSE))) {
        try {
            throw new TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
        } catch (TException e) {
            e.printStackTrace();
        }

    }

    Log.e("------------++++++++++++++++-----------", result);

    return result;
}

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

}

现在您可以像这样从外部执行 ProcessTask,这将确保您异步接收结果:

new ProcessTask(rq, new AsyncResponse() {
    @Override
    public void processFinish(String output) {
        // do whatever you want with the result
    }
}).execute();

关于java - Android 在 AsyncTask onPostExecute 中使用 Interface 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759447/

相关文章:

java - 计算最短马距离的算法(国际象棋)

java - 将 T4CConnection 转换为 OracleConnection 时出错

javascript - 如何用addEventListener替换@click,让addEventListener像@click一样灵活?

java - 我遇到 AsyncTask 类的问题

android - Android 中的 Async Task、Runnable 和 Handler 哪个更好

java - ListView 中的 OnClickListener _id 与 SQLite 中的正确行 _id 不匹配

java - 如何对复杂类进行 Java 单元测试

Android - 用于文本换行图像的 UI

java - 如何使用 Frida 获取 ANDROID_ID

java - 没有回调接口(interface)的AsyncTask