我有一个连接到web服务并返回soapobject的函数。如果不使用asynctask直接调用此函数,则此函数工作正常,但以下代码引发异常。有什么建议吗?
private class LoginTask extends AsyncTask<String, Object, Object> {
protected SoapObject doInBackground(String... params) {
try
{
return callWebService("a","a");
}
catch (Exception e)
{
return null;
}
}
protected void onPostExecute(SoapObject result) {
setErrorText(result);
}
}
private SoapObject callWebService(String UserName, String Password) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("UserName", UserName);
request.addProperty("Password", Password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse(); //this code causes the problem
return response;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (XmlPullParserException e) {
e.printStackTrace();
return null; //Directly jumps to here
}
}
最佳答案
如果您将任务声明更改为:
private class LoginTask extends AsyncTask<String, Object, Object>
到
private class LoginTask extends AsyncTask<String, Object, SoapObject>
您的onPostExecute()应该执行。
关于android - Android ksoap2使用AsyncTask引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13265331/