我有一个设备会按时间间隔向我发送数据,例如5分钟。因此,对于每个数据,我调用以下函数通过webservice将数据发送到接收服务器。一切正常,数据出现两次的唯一问题。我已经彻底检查了每次只调用一次而不是两次的数据。发送每个数据后还有什么问题需要重置或调整?
private static void sendData(String dID,String message) throws Exception {
try
{
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
String url = "http://**********/webservice.asmx?WSDL";
SOAPMessage soapACK = soapConnection.call(createSOAPIncoming(dID,message), url);
printSOAPINCOMING(soapACK,message);
}
catch (Exception e)
{
e.printStackTrace();
System.err.println(e.toString());
}
}
private static SOAPMessage createSOAPIncoming(String dID,String message) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://*******";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
SOAPBodyElement element = body.addBodyElement(envelope.createName("receiveData","", "http://*******"));
element.addChildElement("pt").addTextNode("****");
element.addChildElement("sn").addTextNode(dID);
element.addChildElement("contents").addTextNode(message);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://********");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("\n\n Request SOAP Message To ACK FOR = ");
soapMessage.writeTo(System.out);
System.out.print("\n\n After Print = ");
System.out.println();
return soapMessage;
}
private static void printSOAPINCOMING(SOAPMessage soapResponse,String message) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer1 = transformerFactory.newTransformer();
Source sourceContent1 = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = \n");
StreamResult result1 = new StreamResult(System.out);
transformer1.transform(sourceContent1, result1);
//Connection dbconn = null;
//Statement stat = null;
//ResultSet rsOC1 = null;
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\n\nPrint the soap response:"+ soapResponse.getSOAPPart().getContent());
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMResult result = new DOMResult();
transformer.transform(sourceContent, result);
Document doc = (Document) result.getNode();
String tag = "dResponse";
NodeList list = doc.getElementsByTagName(tag);
// String responseTag = itemTitleNode.getFirstChild().getNodeValue();
Element e = ((Element) list.item(0));
Element eElement = (Element) e.getChildNodes();
//System.out.println("\nCurrent Element :" + e.getNodeName());
String reponseAnswer = eElement.getElementsByTagName("receiveResult").item(0).getTextContent();
System.out.println("\nreposne result :" + reponseAnswer);
if(reponseAnswer.equals("SUCCESS"))
{
}
}