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:
[Java] 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 ");
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"); [java] view plaincopyprint? Document document = incluenthelper. createDocument ();
Element persons = incluenthelper. createElement ("persons ");
Document. add (persons );
Document document = incluenthelper. createDocument ();
Element persons = incluenthelper. createElement ("persons ");
Document. add (persons); [java] view plaincopyprint? Persons. addElement ("address"). addText ("Nanjing ");
Persons. addElement ("id"). addattriement (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 ");
Persons. addElement ("address"). addText ("Nanjing ");
Persons. addElement ("id"). addattriement (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"); [java] view plaincopyprint? 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 return value
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoGet (request, response );
}
Public void init () throws ServletException {
// Put your code here
}
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 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:
[Java] 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 ){
}
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 ){
}