服务器端我使用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,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){
}
总结
以上是内存溢出为你收集整理的Android解析服务器端发来的xml数据示例全部内容,希望文章能够帮你解决Android解析服务器端发来的xml数据示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)