ksoap2soapfault

How to parse soapfault's detail?


I am trying to get the values inside this soap fault's "detail", but I haven't found any ways of doing so.

The response from the server:

 <?xml version="1.0" encoding="UTF-8"?>
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Body>
     <SOAP-ENV:Fault>
       <faultcode>SOAP-ENV:Client</faultcode>
       <faultstring>Many Errors</faultstring>
       <detail>
         <error_id>2</error_id>
         <errors>
           <error>
             <error_id>1</error_id>
             <error_description>Unknown Error</error_description>
           </error>
           <error>
             <error_id>5</error_id>
             <error_description>Not Authorized</error_description>
           </error>
           <error>
             <error_id>9</error_id>
             <error_description>Password should be at least 6 characters including one letter and one number</error_description>
           </error>
         </errors>
       </detail>
     </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

I need to get the error_ids along with their respective error_descriptions. So far I've only managed to get the detail via kSOAP with the following way:

    if (envelope.bodyIn instanceof SoapObject) {
        return envelope.bodyIn.toString();
    } else if (envelope.bodyIn instanceof SoapFault) {
        SoapFault e = (SoapFault) envelope.bodyIn;
        Node details = ((SoapFault) envelope.bodyIn).detail;

    }

but I haven't managed to get a single value I need when I try to "navigate" through it.

Any help is greatly appreciated. I have found little to non information about handling soap faults with ksoap2 online...


Solution

  • Figured it out after all. Here is the way to do it:

            Node details = ((SoapFault) envelope.bodyIn).detail;
            Element detEle = details.getElement(NAMESPACE, "detail");
    
            List<Error> errorList = new ArrayList<NewConnector.Error>();
            Element idEle = detEle.getElement(NAMESPACE, "error_id");
            str.append("id: " + idEle.getText(0));
            str.append("\n");
            Integer id = Integer.valueOf(idEle.getText(0));
            if (id == 2) {
                // many errors
                Element errors = detEle.getElement(NAMESPACE, "errors");
                int errorChildCount = errors.getChildCount();
    
                for (int i = 0; i < errorChildCount; i++) {
                    Object innerError = errors.getChild(i);
    
                    if (innerError instanceof Element) {
    
                        Element error_id = ((Element) innerError).getElement(
                                NAMESPACE, "error_id");
    
                        Element error_descrion = ((Element) innerError)
                                .getElement(NAMESPACE, "error_description");
                        Error singleError = new Error(Integer.valueOf(error_id
                                .getText(0)), error_descrion.getText(0));
    
                        errorList.add(singleError);
                        str.append(singleError.toString() + "\n");
                    }
    
                }
                str.append("Found " + errorList.size() + " errors.\n");
                str.append("errorscount:" + errors.getChildCount());
    

    The code obviously needs improvements, but it's just a showcase of how to get each value. Cheers