Android跟服務器交互數據,有時數據量大時,就需要以xml形式的交互數據。這裡來實現服務器給Android客戶端發送xml數據,Android客戶端解析。
服務器端我使用dom4j第三方包來組織xml數據,大家可自行百度下載。而Android客戶端則使用 XmlPullParser來解析xml數據。
服務器端代碼: 復制代碼 代碼如下:
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("南京");
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("測試");
復制代碼 代碼如下:
String xml = document.asXML(); //用dom4j組織一個XML字符串
response.setContentType("text/xml; charset=UTF-8"); //設置返回值的類型
response.getOutputStream().write(xml.getBytes("UTF-8")); //設置返回值
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
// Put your code here
}
}
Android客戶端: 復制代碼 代碼如下:
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){
}