当前位置:编程学习 > XML/UML >>

Android解析服务器端发来的xml数据

Android跟服务器交互数据,有时数据量大时,就需要以xml形式的交互数据。这里来实现服务器给Android客户端发送xml数据,Android客户端解析。

服务器端我使用dom4j第三方包来组织xml数据,大家可自行百度下载。而Android客户端则使用 XmlPullParser来解析xml数据。

 

服务器端代码:

 

[java]  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");          

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 plaincopyprint?Document document = DocumentHelper.createDocument();  
             Element persons = DocumentHelper.createElement("persons");  
             document.add(persons);  

  Document document = DocumentHelper.createDocument();
               Element persons = DocumentHelper.createElement("persons");
               document.add(persons); [java] view plaincopyprint?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("测试");  

              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 plaincopyprint?        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  

   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]  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();  
      &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,