3

我试图在这个肥皂故障的“细节”中获取值,但我还没有找到任何方法。

来自服务器的响应:

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

我需要得到error_ids 以及他们各自error_description的 s。到目前为止,我只能detail通过以下方式获得 via kSOAP:

    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;

    }

但是当我尝试“导航”它时,我没有设法获得我需要的单个值。

任何帮助是极大的赞赏。我几乎没有发现关于在线使用 ksoap2 处理肥皂故障的信息......

4

3 回答 3

1

下面的代码处理得更好

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();
 ....
}
于 2013-04-22T21:06:29.510 回答
0

我使用以下方法:

/**
 * 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;
}
于 2014-05-13T07:55:12.170 回答
0

毕竟想通了。这是执行此操作的方法:

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

代码显然需要改进,但这只是展示如何获取每个值。干杯

于 2013-04-11T10:15:54.610 回答