我想将此作为 XML 发送到我的 Web 服务。我该怎么做?
<dmi:ShipNoticeRequest xmlns:dmi="http://portal.suppliesnet.net">
<dmi:RequesterISA>xxxxxxxxxx</dmi:RequesterISA>
<dmi:ShipDateRange>
<dmi:ShipDateFrom>2009-09-09</dmi: ShipDateFrom>
<dmi:ShipDateTo>2009-09-10</dmi: ShipDateTo>
</dmi: ShipDateRange >
</dmi:ShipNoticeRequest>
我的 Web 服务方法需要这种类型的请求消息:
POST /ShipNotice/WebServiceShipNotice.asmx HTTP/1.1
Host: portal.suppliesnet.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://portal.suppliesnet.net/RequestShipmentNoticeXML"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<RequestShipmentNoticeXML xmlns="http://portal.suppliesnet.net">
<ShipNoticeRequestNode>xml</ShipNoticeRequestNode>
</RequestShipmentNoticeXML>
</soap:Body>
</soap:Envelope>
我正在尝试这种方法,但遇到未知错误你能弄清楚这段代码有什么问题吗?
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("soap:Envelope");
rootElement.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
rootElement.setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
rootElement.setAttribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
document.appendChild(rootElement);
Element SoapBody = document.createElement("soap:Body");
rootElement.appendChild(SoapBody);
Element RequestShipmentNoticeXML = document.createElement("RequestShipmentNoticeXML");
RequestShipmentNoticeXML.setAttribute("xmlns","http://portal.suppliesnet.net");
SoapBody.appendChild(RequestShipmentNoticeXML);
Element ShipmentNoticeRequestNode = document.createElement("ShipNoticeRequestNode");
RequestShipmentNoticeXML.appendChild(ShipmentNoticeRequestNode);
Element shipNoticeRequest = document.createElement("dmi:ShipNoticeRequest");
shipNoticeRequest.setAttribute("xmlns:dmi", "http://portal.suppliesnet.net");
ShipmentNoticeRequestNode.appendChild(shipNoticeRequest);
Element ContactElement = document.createElement("dmi:RequesterISA");
shipNoticeRequest.appendChild(ContactElement);
ContactElement.appendChild(document.createTextNode("XXXXXX"));
// 1969-12-31
Element articleElement = document.createElement("dmi:ShipDateRange");
Element ShipDateFrom = document.createElement("dmi:ShipDateFrom");
articleElement.appendChild(ShipDateFrom);
ShipDateFrom.appendChild(document.createTextNode("2012-07-06"));
Element ShipDateTo = document.createElement("dmi:ShipDateTo");
articleElement.appendChild(ShipDateTo);
ShipDateTo.appendChild(document.createTextNode("2012-07-06"));
shipNoticeRequest.appendChild(articleElement);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Properties outFormat = new Properties();
outFormat.setProperty(OutputKeys.INDENT, "yes");
outFormat.setProperty(OutputKeys.METHOD, "xml");
outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
outFormat.setProperty(OutputKeys.VERSION, "1.0");
outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperties(outFormat);
DOMSource domSource = new DOMSource(document.getDocumentElement());
OutputStream output = new ByteArrayOutputStream();
StreamResult result = new StreamResult(output);
transformer.transform(domSource, result);
xmlString = output.toString();
} catch (ParserConfigurationException e) {
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
}
SoapObject request = new SoapObject(namespace, methodName);
request.addProperty("RequestShipmentNoticeXMl",xmlString);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
// envelope.headerIn.
final HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(Soap_Action, envelope);
//androidHttpTransport.
SoapObject SoapResult = (SoapObject)envelope.bodyIn;
tv.setText("Status" + SoapResult);
} catch (Exception ex) {
ex.printStackTrace();
Log.e("static", "Exception in making call to server");
}