安卓的AsyncTask不返回正确的结果 [英] Android AsyncTask don't return correct Result

查看:218
本文介绍了安卓的AsyncTask不返回正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我想使用 KSOAP 的AsyncTask 项目之一。现在在这个下面code 的AsyncTask 可正常,但后返回结果为 WSDLHeper 我得到wronge价值的工作。例如在 doInBackground 结果为 this.result 的方法是:

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

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

  ir.tsms.wsdl.ProcessTask@417b65e0
 

在这一行:

  TMP =将String.valueOf(p.execute());
 

和多数民众赞成wronge TMP 必须具有

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

返回。

我的code:

 公共类WSDLHelper {
    公共SoapObject请求;
    公共静态字符串调用(SoapObject RQ){

        字符串TMP;
        ProcessTask P =新ProcessTask(RQ);

        TMP =将String.valueOf(p.execute());

        返回TMP;
    }

}
类ProcessTask扩展的AsyncTask<虚空,虚空,字符串> {
    SoapObject REQ1;
    私人字符串的结果;
    公共ProcessTask(SoapObject RQ){
        REQ1 = RQ;
    }

    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
    }

    @覆盖
    保护字符串doInBackground(空... PARAMS){

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

        AndroidHttpTransport运输=新AndroidHttpTransport(Strings.URL_TSMS);
        transport.debug = TRUE;

        尝试 {
            transport.call(Strings.URL_TSMS + this.req1.getName(),信封);
            。this.result = envelope.getResponse()的toString();
        }赶上(IOException异常前){
            Log.e(,ex.getMessage());
        }赶上(XmlPullParserException前){
            Log.e(,ex.getMessage());
        }

        如果(result.equals(将String.valueOf(整数,code_USER_PASS_FALSE))){
            尝试 {
                抛出新TException(PublicErrorList.USERNAME_PASSWORD_ERROR);
            }赶上(TException E){
                e.printStackTrace();
            }

            返回null;
        }
        返回this.result;
    }

    @覆盖
    保护无效onPostExecute(字符串结果){

        super.onPostExecute(结果);
    }

}
 

解决方案

您不能得到的AsyncTask的返回值由

TMP =将String.valueOf(p.execute());

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

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

您可以参考<一个href="http://stackoverflow.com/questions/4489399/asynctask-where-return-value-of-doinbackground-goes">AsyncTask:其中,doInBackground()的返回值去?了解语法和详细信息。

for one of projects i want to use Ksoap with AsyncTask. now in this below code AsyncTask can work correctly but after return result to WSDLHeper i get wronge value. for example Result for this.result in doInBackground method is :

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

after return value i'm in tmp = String.valueOf(p.execute()) have :

ir.tsms.wsdl.ProcessTask@417b65e0

in this line:

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

and thats wronge tmp must be have

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

to return.

My code:

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

}

解决方案

You cannot get the return value of AsyncTask by

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

This is because Android app doesn't wait for AsyncTask to return a value to move to next line.

You need to use the onPostExecute() Method to process the return value of an AsyncTask .

You can refer AsyncTask: where return value of doInBackground() goes? for more syntax and more information.

这篇关于安卓的AsyncTask不返回正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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