Android解析服务器端发来的xml数据示例
程序员文章站
2024-02-01 23:40:46
android跟服务器交互数据,有时数据量大时,就需要以xml形式的交互数据。这里来实现服务器给android客户端发送xml数据,android客户端解析。服务器端我使用...
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){
}
服务器端我使用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){
}
上一篇: seasar一般性配置