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:

Client:

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: // 172.22.35.112: 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! ");
}
}

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

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.