Android AsyncTask 不返回正确的结果 [英] Android AsyncTask don't return correct Result
问题描述
对于我想将 Ksoap
与 AsyncTask
一起使用的项目之一.现在在下面的代码中 AsyncTask
可以正常工作,但在将结果返回给 WSDLHeper
后,我得到了错误的值.例如 doInBackground
方法中 this.result
的结果是:
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; }]
返回值后我在 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);
}
}
推荐答案
你无法通过
tmp = String.valueOf(p.execute());
这是因为 Android 应用不会等待 AsyncTask 返回值以移动到下一行.
This is because Android app doesn't wait for AsyncTask to return a value to move to next line.
您需要使用 onPostExecute()
方法来处理 AsyncTask 的返回值.
You need to use the onPostExecute()
Method to process the return value of an AsyncTask .
可以参考AsyncTask:doInBackground()的返回值去哪里了? 了解更多语法和更多信息.
You can refer AsyncTask: where does the return value of doInBackground() go? for more syntax and more information.
这篇关于Android AsyncTask 不返回正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!