Android parsing server-side XML Data Sample _android

Source: Internet
Author: User
Android interacts with the server data, and sometimes when the data is large, it requires interactive data in XML form. Here to implement the server to the Android client to send XML data, Android client resolution.

Server side I use DOM4J Third-party package to organize XML data, we can download their own Baidu. The Android client uses Xmlpullparser to parse the XML data.

Server-side code:
Copy Code code as follows:

Package servlet;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.net.URLDecoder;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
public class Getsms extends HttpServlet {
Public Getsms () {
Super ();
}
public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html; Charset=utf-8 ");
Request.setcharacterencoding ("UTF-8");

Copy Code code as follows:

Document document = Documenthelper.createdocument ();
Element persons = documenthelper.createelement ("persons");
Document.add (persons);

Copy Code code as follows:

Persons.addelement ("Address"). AddText ("Nanjing");
Persons.addelement ("id"). AddAttribute (1);
Persons.addelement ("thread_id"). addtext (2);
Persons.addelement ("date"). AddText ("2013");
Persons.addelement ("status"). AddText (-1);
Persons.addelement ("type"). addtext (0);
Persons.addelement ("Body"). AddText ("test");

Copy Code code as follows:

String XML = Document.asxml (); Organize an XML string with dom4j
Response.setcontenttype ("Text/xml; Charset=utf-8 "); Set the type of the return value
Response.getoutputstream (). Write (Xml.getbytes ("UTF-8")); Set return value
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
public void Init () throws Servletexception {
Put your code here
}
}

Android Client:
Copy Code code as follows:

String url= "Http://10.0.2.2:8080/sms_server/servlet/getSms";
try{
URL u = new url (URL);
HttpURLConnection conn = (httpurlconnection) u.openconnection ();
Conn.setrequestmethod ("get");
if (conn.getresponsecode () = = 200) {
InputStream in = Conn.getinputstream ();
Xmlpullparser parser = Xml.newpullparser ();
Parser.setinput (In, "UTF-8");
int event = Parser.geteventtype ();
while (event!= xmlpullparser.end_document) {
LOG.I ("Start_document", "start_document");
Switch (event) {
Case Xmlpullparser.start_tag:
if ("Address". Equals (Parser.getname ()) {
String Address=parser.nexttext ();

}
if ("id". Equals (Parser.getname ()) {
int Id=parser.getattributevalue (0);
}
if (("thread_id"). Equals (Parser.getname ()) {
String Thread_id=parser.nexttext ();
}
if ("Date". Equals (Parser.getname ()) {
String Date=parser.nexttext ();
}
if (("status"). Equals (Parser.getname ()) {
String Status=parser.nexttext ();
}
if ("type". Equals (Parser.getname ()) {
String Type=parser.nexttext ();
}
if ("Body". Equals (Parser.getname ()) {
String Body=parser.nexttext ();
}
Break
Case Xmlpullparser.end_tag:
Break
}
event = Parser.next ();
}
}
}catch (Exception e) {

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