2013-01-23 52 views
0

我仍然對SOAP不熟悉,並且無法解決此問題。 基本上,下面的代碼創建了一條SOAP消息發送給API服務器。 每次運行此請求時,程序總是返回NULL和錯誤代碼,這些代碼不包含在API文檔中。我希望這裏有人能幫忙。使用Globe LBS API的SOAP請求

public class LBSController { 
private static final String endpoint = "http://iplaypen.globelabs.com.ph:1881/axis2/services/Platform"; 

public static void main(String[] args) throws SOAPException { 
    //CREATE SOAP MESSAGE 
    SOAPMessage message = MessageFactory.newInstance().createMessage(); 
    SOAPHeader header = message.getSOAPHeader(); 
    header.detachNode(); 

    //SOAP SETTINGS 
    SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); 
    envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); 

    SOAPBody body = message.getSOAPBody(); 
    QName bodyName = new QName("getConsent"); 
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 

    //SET CONTENT 
    SOAPElement uName = bodyElement.addChildElement("uName"); 
    uName.addTextNode("k2r2t1zvc"); 
    SOAPElement uPin = bodyElement.addChildElement("uPin"); 
    uPin.addTextNode("21737629"); 
    SOAPElement MSISDN = bodyElement.addChildElement("MSISDN"); 
    MSISDN.addTextNode("09278328310"); 

    //CREATE CONNECTION 
    SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); 
    SOAPMessage response = connection.call(message, endpoint); 
    connection.close(); 

    SOAPBody responseBody = response.getSOAPBody(); 
    SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next(); 
    SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next(); 

    if(responseBody.getFault()!=null){ 
     System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString()); 
    } else { 
     System.out.println(returnElement.getValue()); 
    } 

    try { 
     System.out.println(getXmlFromSOAPMessage(message)); 
     System.out.println(getXmlFromSOAPMessage(response)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException { 
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); 
    msg.writeTo(byteArrayOS); 
    return new String(byteArrayOS.toByteArray()); 
} 

}

回答

0

嘗試從響應打印出原始的XML代碼。

下面是代碼

public class LBSController 
{ 
    private static final String endpoint = "http://iplaypen.globelabs.com.ph:1881/axis2/services/Platform"; 

public static void main(String[] args) throws SOAPException { 
//CREATE SOAP MESSAGE 
SOAPMessage message = MessageFactory.newInstance().createMessage(); 
SOAPHeader header = message.getSOAPHeader(); 
header.detachNode(); 

//SOAP SETTINGS 
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); 
envelope.setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/"); 

SOAPBody body = message.getSOAPBody(); 
QName bodyName = new QName("getConsent"); 
SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 

//SET CONTENT 
SOAPElement uName = bodyElement.addChildElement("uName"); 
uName.addTextNode("k2r2t1zvc"); 
SOAPElement uPin = bodyElement.addChildElement("uPin"); 
uPin.addTextNode("21737629"); 
SOAPElement MSISDN = bodyElement.addChildElement("MSISDN"); 
MSISDN.addTextNode("09278328310"); 

//CREATE CONNECTION 
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection(); 
SOAPMessage response = connection.call(message, endpoint); 
connection.close(); 

SOAPBody responseBody = response.getSOAPBody(); 
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next(); 
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next(); 

if(responseBody.getFault()!=null){ 
    System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString()); 
} else { 
    System.out.println(returnElement.getValue()); 
} 

//PRINT OUT RAW XML 
ByteArrayOutputStream out = new ByteArrayOutputStream(); 
String xml = ""; 
try { 
    response.writeTo(out); 
    xml = out.toString("UTF-8"); 
} catch (Exception e) 
{ 
    System.out.println(""+e); 
    //log.error(e.getMessage(),e); 
}  
System.out.println(""+xml); 

/* 
try { 
    System.out.println(getXmlFromSOAPMessage(message)); 
    System.out.println(getXmlFromSOAPMessage(response)); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
* */ 
} 

從代碼的響應值以上

run: 
    null 
    <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
<soapenv:Body><ns:getLocResponse xmlns:ns="http://ESCPlatform/xsd"> 
<ns:LocationReturn><ns:return>506</ns:return><ns:tranId>23737563092783283102013</ns:tranId> 
</ns:LocationReturn></ns:getLocResponse> 
</soapenv:Body> 
</soapenv:Envelope> 
    BUILD SUCCESSFUL (total time: 3 seconds) 
+0

您好,感謝您的答覆,我已經解決了這個問題。似乎在「http:// ESCPlatform/xsd」的聲明中存在問題。不管怎麼說,還是要謝謝你。 –