Android通過post請求發送一個xml,解析返回xml資料

來源:互聯網
上載者:User

 

工作的時候需要往後台發送一個post資料請求

 

其中發送的xml資料為:

<?xml version = “1.0” ?> <SSOMessage version=”1.0”><SSOParas><SeqID>SeqID</SeqID> <CommandID>CommandID</CommandID> <MSISDN>ABSCDSDF</MSISDN><ChargeMSISDN>ChargeMSISDN</ChargeMSISDN><SPID>SPID</SPID><Code> Code </ Code >< IDtype > IDtype 0</ IDtype ><ID> ID 0</ID></SSOParas></SSOMessage>

返回的xml資料為:

<?xml version = “1.0” ?> <SSOMessage version=”1.0”> <SSOParas> <SeqID>SeqID</SeqID> <ResultCode>ResultCode0</ResultCode></SSOParas></SSOMessage>

然後進行解析,代碼如下,參考一下,對於以後再做post請求的時候,做參考

class httpThread implements Runnable {    /* (non-Javadoc)     * @see java.lang.Runnable#run()     */    @Override    public void run() {        // TODO Auto-generated method stub        //組建xml資料        StringBuilder xml = new StringBuilder();        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");        xml.append("<SSOMessage version=\"1.0\">");        xml.append("<SSOParas>");        xml.append("<SeqID>13333333333</SeqID>");        xml.append("<CommandID>1</CommandID>");        xml.append("<MSISDN>1333333333</MSISDN>");        xml.append("<ChargeMSISDN>1333333333</ChargeMSISDN>");        xml.append("<SPID>3510127</SPID>");        xml.append("<Code></Code>");        xml.append("<IDtype>0</IDtype>");        xml.append("<ID>135000000000000216559</ID>");        xml.append("</SSOParas>");        xml.append("</SSOMessage>");        try {            byte[] xmlbyte = xml.toString().getBytes("UTF-8");                        System.out.println(xml);            URL url = new URL("http://118.85.194.28:8080/sotpms_server/GetSSOMessage");                                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();            conn.setConnectTimeout(5000);            conn.setDoOutput(true);// 允許輸出            conn.setDoInput(true);            conn.setUseCaches(false);// 不使用緩衝            conn.setRequestMethod("POST");            conn.setRequestProperty("Connection", "Keep-Alive");// 維持長串連            conn.setRequestProperty("Charset", "UTF-8");            conn.setRequestProperty("Content-Length",                    String.valueOf(xmlbyte.length));            conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");            conn.setRequestProperty("X-ClientType", "2");//發送自訂的頭資訊            conn.getOutputStream().write(xmlbyte);            conn.getOutputStream().flush();            conn.getOutputStream().close();            if (conn.getResponseCode() != 200)                throw new RuntimeException("請求url失敗");            InputStream is = conn.getInputStream();// 擷取返回資料              
            // 使用輸出資料流來輸出字元(可選)            ByteArrayOutputStream out = new ByteArrayOutputStream();            byte[] buf = new byte[1024];            int len;            while ((len = is.read(buf)) != -1) {                out.write(buf, 0, len);            }            String string = out.toString("UTF-8");            System.out.println(string);            out.close();                                                  // xml解析            String version = null;            String seqID = null;            XmlPullParser parser = Xml.newPullParser();            try {                parser.setInput(new ByteArrayInputStream(string.substring(1)                        .getBytes("UTF-8")), "UTF-8");                 parser.setInput(is, "UTF-8");                int eventType = parser.getEventType();                while (eventType != XmlPullParser.END_DOCUMENT) {                    if (eventType == XmlPullParser.START_TAG) {                        if ("SSOMessage".equals(parser.getName())) {                            version = parser.getAttributeValue(0);                        } else if ("SeqID".equals(parser.getName())) {                            seqID = parser.nextText();                        } else if ("ResultCode".equals(parser.getName())) {                            resultCode = parser.nextText();                        }                    }                    eventType = parser.next();                }            } catch (XmlPullParserException e) {                e.printStackTrace();                System.out.println(e);            } catch (IOException e) {                e.printStackTrace();                System.out.println(e);            }            System.out.println("version = " + version);            System.out.println("seqID = " + seqID);            System.out.println("resultCode = " + resultCode);*/        } catch (Exception e) {            // TODO Auto-generated catch block            System.out.println(e);        }    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.