我对ksoap Parsing有一点了解。当我解析一些数据时,它会给出错误:
java.lang.ClassCastException: org.ksoap2.SoapFault
我可以在SoapUI中看到该方法的响应,但是当我在android中解析该方法时,它会产生如上所述的错误。
这是Request参数作为SoapUI中的输入
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:SaveChangePasswordForExternalUser>
<tem:userId>Test123</tem:userId>
<!--Optional:-->
<tem:oldPassword>TestTest</tem:oldPassword>
<!--Optional:-->
<tem:newPassword>Test</tem:newPassword>
<!--Optional:-->
<tem:retypedNewPassword>Test</tem:retypedNewPassword>
</tem:SaveChangePasswordForExternalUser>
</soapenv:Body>
</soapenv:Envelope>
这是我在SoapUI中得到的回应
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
<faultstring xml:lang="en-US">Old Password was incorrectly entered. Remember that passwords are case-sensitive.</faultstring>
<detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"/>
<InnerException i:nil="true"/>
<Message>Old Password was incorrectly entered. Remember that passwords are case-sensitive.</Message>
<Type>System.Web.Services.Protocols.SoapException</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
这是我如何尝试通过android
中的代码获得响应public void getData()
{
private static final String NAMESPACE = "http://tempuri.org/"; //
private static final String URL = "http://173.203.136.194:99/LeaseWave.MobileApplication.Service/MobileApplicationService.svc/basic";
private static final String SOAP_ACTION = "http://tempuri.org/IMobileApplicationService/SaveChangePasswordForExternalUser";
private static final String METHOD_NAME = "SaveChangePasswordForExternalUser";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("userId", "Test123");
request.addProperty("oldPassword", "TestTest");
request.addProperty("newPassword", "Test");
request.addProperty("newPassword", "Test");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
Log.i("Long...", "Response ------ "+resultsRequestSOAP);
}catch (Exception e)
{
e.printStackTrace();
}
}
但我没有成功。 任何人都有这种暗示或解决方案吗?
答案 0 :(得分:9)
当您处理SOAP Web服务时,这个问题可能会有一段时间。 来自服务的响应可以是 SOAP对象,如果出现错误的凭据,那么响应会出现错误信息并且它是 SOAPFAULT对象。因此,请更新解析代码以检查响应对象的类型。
这种代码可以解决您的问题,
if (envelope.bodyIn instanceof SoapFault) {
String str= ((SoapFault) envelope.bodyIn).faultstring;
Log.i("", str);
// Another way to travers through the SoapFault object
/* Node detailsString =str= ((SoapFault) envelope.bodyIn).detail;
Element detailElem = (Element) details.getElement(0)
.getChild(0);
Element e = (Element) detailElem.getChild(2);faultstring;
Log.i("", e.getName() + " " + e.getText(0)str); */
} else {
SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
Log.d("WS", String.valueOf(resultsRequestSOAP));
}
答案 1 :(得分:0)
请进行更改:::
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("tem:userId", UserId);
request.addProperty("tem:oldPassword",OldPasssword);
request.addProperty("tem:newPassword", NewPassword);
request.addProperty("tem:retypedNewPassword", NewPasswordAgain);
您需要传递确切的标记名称以将值放在Webservice的方法中,就像将值指定为方法的精确参数名称一样。
如果还有其他问题,请与我们联系。
答案 2 :(得分:0)
我相信错误在这里:
Log.i("Long...", "Response ------ "+resultsRequestSOAP);
ClassCastException可能是因为将SOAP对象“resultsRequesSOAP”静默转换为字符串。我不确定.toString()是否可以工作,但尝试返回一些内容并使用SOAPObject的getProperty()方法获取它。看看是否有效。
如果没有,请发布错误日志并调试,看看究竟是什么问题。哪条线...