KSOAP解析错误: - java.lang.ClassCastException:org.ksoap2.SoapFault

Yog*_*uru 2 java android ksoap

我对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>
Run Code Online (Sandbox Code Playgroud)

这是我在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>
Run Code Online (Sandbox Code Playgroud)

这是我如何尝试通过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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

但我没有成功.任何人都有这种暗示或解决方案吗?

MKJ*_*ekh 9

当您处理SOAP Web服务时,这个问题可能会来一段时间.来自服务的响应可以是SOAP对象,如果出现错误,则传递错误的凭据,然后Response会出现错误消息并且它是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));
    }
Run Code Online (Sandbox Code Playgroud)