Android parses XML data sent from the server

Source: Internet
Author: User

Android interacts with the server. Sometimes, when the data volume is large, interactive data in XML format is required. Here, the server sends XML data to the android client for parsing.

On the server side, I use a third-party package of dom4j to organize XML data. You can download the data by Baidu. The android client uses xmlpullparser to parse XML data.

 

Server code:

 

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");
Document document = DocumentHelper.createDocument();                Element persons = DocumentHelper.createElement("persons");                document.add(persons); 
Persons. addelement ("Address "). addtext ("Nanjing"); persons. addelement ("ID "). addattrisons (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 ");
String xml = document. asxml (); // use dom4j to organize an XML string response. setcontenttype ("text/XML; charset = UTF-8"); // set the type of the returned value response. getoutputstream (). write (XML. getbytes ("UTF-8"); // set the returned 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:

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.