Android sends an XML file through the POST request to parse and return XML data.

Source: Internet
Author: User

 

When working, you need to send a post data request to the background

 

The XML data sent is:

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

The returned XML data is:

<?  XML version = "1.0"  ?>   <  Ssomessage  Version  = "1.0">  <Ssoparas  >   <  Seqid  > Seqid </  Seqid  >   <  Resultcode > Resultcode0 </  Resultcode  >  </  Ssoparas  >  </  Ssomessage  > 

Then parse,CodeFor more information, see

 Class Httpthread Implements  Runnable {  /* (Non-javadoc) * @ see java. Lang. runnable # Run ()  */  @ Override  Public   Void  Run (){  //  Todo auto-generated method stub  //  Construct XML data 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 ); //  Allow output Conn. setdoinput ( True  ); Conn. setusecaches (  False ); //  No Cache Conn. setrequestmethod ("Post"); Conn. setrequestproperty ( "Connection", "keep-alive "); //  Maintain persistent connections Conn. setrequestproperty ("charset", "UTF-8" ); Conn. setrequestproperty ( "Content-Length" , String. valueof (xmlbyte. Length); Conn. setrequestproperty ( "Content-Type", "text/XML; charsets = UTF-8" ); Conn. setrequestproperty ( "X-clienttype", "2 "); //  Send custom header information Conn. getoutputstream (). Write (xmlbyte); Conn. getoutputstream (). Flush (); Conn. getoutputstream (). Close ();  If (Conn. getresponsecode ()! = 200 )  Throw   New Runtimeexception ("request URL failed" ); Inputstream is = Conn. getinputstream (); //  Get returned data 
   //  Use the output stream to output characters (optional) Bytearrayoutputstream out =New  Bytearrayoutputstream ();  Byte [] Buf = New   Byte [1024 ];  Int  Len;  While (LEN = is. Read (BUF ))! =-1 ) {Out. Write (BUF, 0 , Len);} string = Out. tostring ("UTF-8"); System. Out. println (string); Out. Close ();  //  XML Parsing 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 );}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.