Android解析伺服器端發來的xml資料

來源:互聯網
上載者:User

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();  
           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){ 
         
    } 

 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){
   
  }
    

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.