Android interacts with servers. Sometimes when the data volume is large, interactive data in the form of xml is needed. Here we implement the server to send xml data to the Android client and parse the Android client.
On the server side, I use the dom4j third-party package to organize XML data, and everyone can download it on Baidu. The Android client uses XmlPullParser to parse xml data.
Server-side code:
package servlet;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import org.;
public class getSms extends HttpServlet {
public getSms() {
super();
}
public void destroy() {
(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
("text/html; charset=UTF-8" );
("UTF-8");
Document document = ();
Element persons = ("persons");
(persons);
("address").addText("Nanjing");
("id").addAttribute(1);
("thread_id").addText(2);
("date").addText("2013");
("status").addText(-1);
("type").addText(0);
("body").addText("test");
String xml = (); //Use dom4j to organize an XML string
("text/xml; charset=UTF-8"); //Set the type of return value
().write(("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:
String url="http://10.0.2.2:8080/sms_server/servlet/getSms";
try{
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ();
("GET");
if (() == 200) {
InputStream in = ();
XmlPullParser parser = ();
(in, "UTF-8");
int event = ();
while (event != XmlPullParser.END_DOCUMENT) {
("start_document", "start_document");
switch (event) {
case XmlPullParser.START_TAG:
if ("address".equals(())) {
String address=();
}
if ("id".equals(())) {
int id=(0);
}
if (("thread_id").equals(())) {
String thread_id=();
}
if ("date".equals(())) {
String date=();
}
if (("status").equals(())) {
String status=();
}
if ("type".equals(())) {
String type=();
}
if ("body".equals(())) {
String body=();
}
break;
case XmlPullParser.END_TAG:
break;
}
event = ();
}
}
}catch(Exception e){
}
On the server side, I use the dom4j third-party package to organize XML data, and everyone can download it on Baidu. The Android client uses XmlPullParser to parse xml data.
Server-side code:
Copy the codeThe code is as follows:
package servlet;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import org.;
public class getSms extends HttpServlet {
public getSms() {
super();
}
public void destroy() {
(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
("text/html; charset=UTF-8" );
("UTF-8");
Copy the codeThe code is as follows:
Document document = ();
Element persons = ("persons");
(persons);
Copy the codeThe code is as follows:
("address").addText("Nanjing");
("id").addAttribute(1);
("thread_id").addText(2);
("date").addText("2013");
("status").addText(-1);
("type").addText(0);
("body").addText("test");
Copy the codeThe code is as follows:
String xml = (); //Use dom4j to organize an XML string
("text/xml; charset=UTF-8"); //Set the type of return value
().write(("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:
Copy the codeThe code is as follows:
String url="http://10.0.2.2:8080/sms_server/servlet/getSms";
try{
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ();
("GET");
if (() == 200) {
InputStream in = ();
XmlPullParser parser = ();
(in, "UTF-8");
int event = ();
while (event != XmlPullParser.END_DOCUMENT) {
("start_document", "start_document");
switch (event) {
case XmlPullParser.START_TAG:
if ("address".equals(())) {
String address=();
}
if ("id".equals(())) {
int id=(0);
}
if (("thread_id").equals(())) {
String thread_id=();
}
if ("date".equals(())) {
String date=();
}
if (("status").equals(())) {
String status=();
}
if ("type".equals(())) {
String type=();
}
if ("body".equals(())) {
String body=();
}
break;
case XmlPullParser.END_TAG:
break;
}
event = ();
}
}
}catch(Exception e){
}