Question

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...

Was it helpful?

Solution 2

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

OTHER TIPS

The following code handles better

Iterator it = soapFaultClientException.getSoapFault().getFaultDetail().getDetailEntries();
while (it.hasNext())
{
 Source errSource = it.next().getSource();
 @SuppressWarnings("unchecked")
 JAXBElement errJaxb = (JAXBElement) springWebServiceTemplate.getUnmarshaller().unmarshal(errSource);
 ServerCustomizedError err = errJaxb.getValue();
 ....
}

I use the following method:

/**
 * Method to retrieve the errorMessage from the given SoapFault.
 * @param soapFault
 * @return String representing the errorMessage found in the given SoapFault.
 */
private static String getSoapErrorMessage (SoapFault soapFault) {
    String errorMessage;
    try {
        Node detailNode = soapFault.detail;
        Element faultDetailElement = (Element)detailNode.getElement(0).getChild(1);
        Element errorMessageElement = (Element)faultDetailElement.getChild(0);
        errorMessage =  errorMessageElement.getText(0);
    }
    catch (Exception e) {
        e.printStackTrace();
        errorMessage = "Could not determine soap error.";
    }
    return errorMessage;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top