2

我对 ksoap Parsing 有一点了解。当我解析一些数据时,它会给出错误:

java.lang.ClassCastException: org.ksoap2.SoapFault

我可以在SoapUI中看到该方法的响应,但是当我在 android 中解析该方法时,它会出现如上所述的错误。

这是 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();
        }

    }

但我没有得到成功。有人对此有任何提示或解决方案吗?

4

3 回答 3

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));
    }
于 2012-06-04T07:19:15.457 回答
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);

您需要传递准确的标记名称才能将值放入 Web 服务的方法中,就像将值指定为方法的准确参数名称一样。

让我知道是否还有其他问题。

于 2012-06-01T10:18:19.910 回答
0

我相信错误在这里:

Log.i("Long...", "Response  ------ "+resultsRequestSOAP);

ClassCastException 可能是因为将 SOAP 对象“resultsRequesSOAP”静默转换为字符串。我不确定 .toString() 是否有效,但尝试返回一些内容并使用 SOAPObject 的 getProperty() 方法获取它。看看这是否有效。

如果不是,请发布错误日志并进行调试,看看问题到底出在哪里。哪条线...

于 2012-06-01T11:03:52.497 回答