Error in KSOAP Parsing :- java.lang.ClassCastException: org.ksoap2.SoapFault

Yog Guru picture Yog Guru · Jun 1, 2012 · Viewed 7.3k times · Source

I have a little knowledge in ksoap Parsing. When i am parsing some data it gives Error :

java.lang.ClassCastException: org.ksoap2.SoapFault

I can see the response for that method in SoapUI but when i parsing that method in android it gives an error as like above.

Here is Request parameter as input in 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>

Here Is Response i got in 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>

This how i try to get the response by code in 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();
        }

    }

but i don't get success in that. Anyone have any kind of hint or solution for this?

Answer

MKJParekh picture MKJParekh · Jun 4, 2012

when you are dealing with SOAP Web Service, This problem may come some time. The response coming from the service can either be a SOAP Object and if something goes wrong like wrong credentials passed then Response comes with error message and it's a SOAPFAULT Object. So update your code of parsing to check the type of the response object.

This sort of code can solve your problem,

    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));
    }