在Android中发送XML到Web服务 [英] Sending XML to a Web Service in Android

查看:108
本文介绍了在Android中发送XML到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送这是XML到我的Web服务。我应该怎么办呢?

 < D​​MI:ShipNoticeRequest的xmlns:DMI =htt​​p://portal.suppliesnet.net>
< D​​MI:RequesterISA> XXXXXXXXXX< / DMI:RequesterISA>
< D​​MI:ShipDateRange>
< D​​MI:ShipDateFrom> 2009-09-09< / DMI:ShipDateFrom>
< D​​MI:ShipDateTo> 2009-09-10< / DMI:ShipDateTo>
< / DMI:ShipDateRange>
< / DMI:ShipNoticeRequest>
 

我的Web服务方法需要这种类型的请求消息的:

  POST /ShipNotice/WebServiceShipNotice.asmx HTTP / 1.1
主持人:portal.suppliesnet.net
内容类型:text / xml的;字符集= UTF-8
内容长度:长度
SOAPAction报:http://portal.suppliesnet.net/RequestShipmentNoticeXML

< XML版本=1.0编码=UTF-8&GT?;
<肥皂:信封的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema的xmlns:肥皂= http://schemas.xmlsoap.org/soap/envelope/">
  <肥皂:身体与GT;
    < RequestShipmentNoticeXML的xmlns =htt​​p://portal.suppliesnet.net>
      < ShipNoticeRequestNode> XML< / ShipNoticeRequestNode>
    < / RequestShipmentNoticeXML>
  < / SOAP:身体与GT;
< /肥皂:信封>
 


我特灵这种方法,但你能弄清楚什么是不对的code geeting未知错误?

 尝试{
            的DocumentBuilderFactory的DocumentBuilderFactory =的DocumentBuilderFactory
                    .newInstance();
            的DocumentBuilder的DocumentBuilder =的DocumentBuilderFactory
                    .newDocumentBuilder();
            文献文件= documentBuilder.newDocument();
            元rootElement的= document.createElement方法(肥皂:信封);
            rootElement.setAttribute(的xmlns:XSI,http://www.w3.org/2001/XMLSchema-instance);
            rootElement.setAttribute(的xmlns:XSD,http://www.w3.org/2001/XMLSchema);
            rootElement.setAttribute(的xmlns:香皂,http://schemas.xmlsoap.org/soap/envelope/);


            document.appendChild(rootElement的);

            元素的SOAPBody = document.createElement方法(肥皂:身体);
            rootElement.appendChild(的SOAPBody);

            元件RequestShipmentNoticeXML = document.createElement方法(RequestShipmentNoticeXML);
            RequestShipmentNoticeXML.setAttribute(的xmlns,http://portal.suppliesnet.net);
            SoapBody.appendChild(RequestShipmentNoticeXML);

            元件ShipmentNoticeRequestNode = document.createElement方法(ShipNoticeRequestNode);
            RequestShipmentNoticeXML.appendChild(ShipmentNoticeRequestNode);

            元件shipNoticeRequest = document.createElement方法(DMI:ShipNoticeRequest);
            shipNoticeRequest.setAttribute(的xmlns:DMI,http://portal.suppliesnet.net);

            ShipmentNoticeRequestNode.appendChild(shipNoticeRequest);

            元件ContactElement = document.createElement方法(DMI:RequesterISA);
            shipNoticeRequest.appendChild(ContactElement);
            ContactElement.appendChild(document.createTextNode(XXXXXX));
// 1969-12-31


            元件articleElement = document.createElement方法(DMI:ShipDateRange);

            元件ShipDateFrom = document.createElement方法(DMI:ShipDateFrom);
            articleElement.appendChild(ShipDateFrom);
            ShipDateFrom.appendChild(document.createTextNode(2012-07-06));

            元件ShipDateTo = document.createElement方法(DMI:ShipDateTo);
            articleElement.appendChild(ShipDateTo);
            ShipDateTo.appendChild(document.createTextNode(2012-07-06));

            shipNoticeRequest.appendChild(articleElement);


            工厂的TransformerFactory = TransformerFactory.newInstance();
            变压器变压器= factory.newTransformer();
            属性outFormat =新特性();

            outFormat.setProperty(OutputKeys.INDENT,是);
            outFormat.setProperty(OutputKeys.METHOD,XML);
            outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION,无);
            outFormat.setProperty(OutputKeys.VERSION,1.0);
            outFormat.setProperty(OutputKeys.ENCODING,UTF-8);
            transformer.setOutputProperties(outFormat);
            DOMSource的DOMSource的=新DOMSource的(document.getDocumentElement());
            的OutputStream输出=新ByteArrayOutputStream();
            StreamResult结果=新StreamResult(输出);
            transformer.transform(DOMSource的,结果);
            的xmlString = output.toString();

        }赶上(的ParserConfigurationException E){
        }赶上(TransformerConfigurationException E){
        }赶上(TransformerException中五){
        }

        SoapObject请求=新SoapObject(命名空间,方法名);
        request.addProperty(RequestShipmentNoticeXMl的xmlString);

        SoapSerializationEnvelope包=新SoapSerializationEnvelope(
                SoapEnvelope.VER11);

        envelope.dotNet = TRUE;
        envelope.setOutputSoapObject(要求);
        // envelope.headerIn。
        最后HttpTransportSE androidHttpTransport =新HttpTransportSE(URL);

        androidHttpTransport.debug = TRUE;
        尝试 {
            androidHttpTransport.call(Soap_Action,包);
            // androidHttpTransport。
             SoapObject健胃presult =(SoapObject)envelope.bodyIn;
            tv.setText(状态+健胃presult);
        }赶上(例外前){
            ex.printStackTrace();
            Log.e(静,在作出调用服务器异常);
        }
 

解决方案

您想发送SOAP味精和Android不具备发送这些类型的消息直接支持。有喜欢KSOAP2,机器人,您可以尝试第三方库。
应该如何每个SOAP味精看起来像在服务器端的计算公式如下WSDL文件。试图找到它的服务器上,并期待每个MSG应该如何看。您可以使用soapUI的解析WSDL文件给你。
如果你不使用任何第三方库如上所述,你必须通过你的自我组成的味精。您将需要建立某种形式的HTTP客户端是这样的:

 的HttpURLConnection康恩= NULL;
康恩=(HttpURLConnection类)新的网址(URL).openConnection();
conn.setRequestMethod(POST);
conn.setRequestProperty(内容类型,为text / xml);
conn.setRequestProperty(内容长度,+ postMessageInBytes.length);
 

请求机构将使用例如XmlSerializer的,这将有助于您编写需要标签等创建。

I want to send this as XML to my Web Service. How should I do that?

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

My Web Service method required this type of request message:

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> 


I am tring this method but geeting an unknown error Can you figure out what is wrong with this code?

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");
        }

解决方案

You want to send SOAP msg and Android don't have direct support for sending those kind of messages. There are 3rd party libraries like KSOAP2-android which you can try.
How should each SOAP msg look like is given by WSDL file on server side. Try to find it on the server and look how each msg should look. You can use soapUI to parse WSDL file for you.
If you don't use any 3rd party library as mentioned above you have to compose that msg by your self. You will need to create some sort of HTTP client like this:

HttpURLConnection conn = null;
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Content-Length", "" + postMessageInBytes.length);

Body of request will be created using for example XmlSerializer which will help you to write needed tags etc.

这篇关于在Android中发送XML到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆