Android AsyncTask 不返回正确的结果

标签 android android-asynctask android-ksoap2

对于其中一个项目,我想将 KsoapAsyncTask 一起使用。现在在下面的代码中 AsyncTask 可以正常工作,但是在将结果返回到 WSDLHeper 之后我得到了错误的值。例如 doInBackground 方法中 this.result 的结果是:

[1, 4674070, {item=3000738; item=TSMS; item=30007227; item=30004444440000; item=30007227001401; item=50004066569100; item=50001717; item=500017171; item=5000171717; item=50001717007227; }]

返回值后我在 tmp = String.valueOf(p.execute()) 有:

ir.tsms.wsdl.ProcessTask@417b65e0

在这一行中:

tmp = String.valueOf(p.execute());

那是错误的 tmp 必须有

[1, 4674070, {item=3000738; item=TSMS; item=30007227; item=30004444440000; item=30007227001401; item=50004066569100; item=50001717; item=500017171; item=5000171717; item=50001717007227; }]

返回。

我的代码:

public class WSDLHelper {
    public SoapObject request;
    public static String call(SoapObject rq){

        String tmp;
        ProcessTask p =new ProcessTask(rq);

        tmp = String.valueOf(p.execute());

        return tmp;
    }

}
class ProcessTask extends AsyncTask<Void, Void, String > {
    SoapObject req1;
    private String result;
    public ProcessTask(SoapObject rq) {
        req1 = rq;
    }

    @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;

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

            return null;
        }
        return this.result;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);
    }

}

最佳答案

不能通过以下方式获取AsyncTask的返回值

tmp = String.valueOf(p.execute());

这是因为 Android 应用程序不等待 AsyncTask 返回值以移动到下一行。

您需要使用 onPostExecute() 方法来处理 AsyncTask 的返回值。

可以引用AsyncTask: where does the return value of doInBackground() go?更多语法和更多信息。

关于Android AsyncTask 不返回正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25758236/

相关文章:

java - 使用 KSOAP2 序列化要发送的整数数组

android - 使用 KSOAP2 根据 OASIS WS 安全规范生成复杂的 SOAP 信封?

java - 更改自定义适配器中按钮的可见性

以错误的顺序安装应用程序时,Android 使用权限问题

android - 从 webservice 对象获取数组项到 android

android - 实时过滤ListView

android - 在应用程序中放置 Asynctask 的位置

android - 使用 copyFromRealm 将 RealmResults<E> 转换为 List<E>

android - 自定义 attr 获取颜色返回无效值

android - AsyncTask 没有锁定主线程?