2013-04-11 3 views
3

이 비누 결함의 "세부 사항"안에 값을 가져 오려고하는데, 그렇게하는 방법을 찾지 못했습니다.soapfault의 세부 사항을 분석하는 방법은 무엇입니까?

서버의 응답 :

<?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는 해당 error_description들과 함께 error_id의를 얻을 필요가있다. 지금까지 나는 단지 다음과 같은 방법으로 kSOAP를 통해 detail를 얻을 관리했습니다 :

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 온라인으로 비누 결함 처리에 관한 정보는 거의 찾아 내지 못했습니다 ...

답변

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

코드는 분명히 개선이 필요는하지만, 각 값을 얻는 방법의 단지 쇼케이스입니다. 건배

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(); 
.... 
} 
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; 
} 
관련 문제