Android跟服务器交互数据,有时数据量大时,就需要以xml形式的交互数据。这里来实现服务器给Android客户端发送xml数据,Android客户端解析。
服务器端我使用dom4j第三方包来组织xml数据,大家可自行百度下载。而Android客户端则使用 XmlPullParser来解析xml数据。
服务器端代码:
[java] view plaincopy
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 plaincopy
Document document = DocumentHelper.createDocument();
Element persons = DocumentHelper.createElement("persons");
document.add(persons);
[java] view plaincopy
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("测试");
[java] view plaincopy
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客户端:
[java] view plaincopy
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){
}
代码coding太麻烦了,后台可以写一个Server用来连接服务器以便检测是否有更新,如果有更新的话服务器端会提供好端口,直接去服务器把数据流存在本地xml文档里就可以了。