Android sends XML data to the server over HTTP

Source: Internet
Author: User
You can load or read an XML file to obtain its data, and then treat the obtained data as an entity, and send it to the server using the output stream through the HTTP protocol, on the server side, you can obtain the relevant data by obtaining the input stream. In this way, you can send XML data to the server. As follows:


Public void sendxmltest () throws exception {
// Load XML resources through the Class Loader
Inputstream = This. getclass (). getclassloader (). getresourceasstream ("test. xml ");
Byte [] xml = streamtool. Read (inputstream );

String Path = "http: // 8080/videonews/getxmlinfo ";
URL url = new URL (PATH );
Httpurlconnection conn = (httpurlconnection) URL. openconnection ();
Conn. setrequestmethod ("Post ");
Conn. setconnecttimeout (5000 );
Conn. setdooutput (true );

// Set the HTTP request header field
Conn. setrequestproperty ("Content-Type", "text/XML; charset = UTF-8"); // content type
Conn. setrequestproperty ("Content-Length", String. valueof (XML. Length); // The length of the object content

Conn. getoutputstream (). Write (XML); // write data to the server through the output stream
If (conn. getresponsecode () = 200 ){
System. Out. println ("sent successfully! ");
} Else {
System. Out. println ("failed to send! ");

  1. Byte [] xml = streamtool. Read (request. getinputstream (); // get the output stream
  2. System. Out. println (new string (XML, "UTF-8 "));
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.