Android data is converted to Xml for the client and the client to use xml data.

Source: Internet
Author: User
Tags xml parser

Android data is converted to Xml for the client and the client to use xml data.

(1) create an object class News

Public class News {private Integer id; private String title; private Integer timelength; public News (Integer id, String title, Integer timelength) {this. id = id; this. title = title; this. timelength = timelength;} // get, set Method}

(2) Implementation classes of interfaces created for interface programming:

Public interface VideoNewsService {/*** get the latest video information * @ return */public List
  
   
GetLastNews ();}
  

Interface implementation class, used to add some false data

Public class VideoNewsServiceBean implements VideoNewsService {public List
  
   
GetLastNews () {List
   
    
Newes = new ArrayList
    
     
(); Newes. add (new News (90, "Pleasant goat and Big Wolf", 78); newes. add (new News (10, "real-time helicopter rescue drills in the East China Sea", 28); newes. add (new News (56, "Cameroon VS Holland", 70); return newes ;}}
    
   
  

(3) return data to the big client through servlet

Public class ListServlet extends HttpServlet {private static final long serialVersionUID = 1L; private VideoNewsService service = new VideoNewsServiceBean (); protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {List
  
   
Videos = service. getLastNews (); request. setAttribute ("videos", videos); // redirects a jsp interface. When the client requests, the returned jsp page data request is sent. getRequestDispatcher ("/WEB-INF/page/videonews. jsp "). forward (request, response );}}
  

(4) generate the jsp page of the xml file (here the returned xml file is contentType = "text/xml ")

<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  
  
       
    
             
     
      ${video.timelength}
         
        
   
  

(5) use xml files in the client
The same client also needs a News entity class, which is omitted here
Create a service class for parsing xml and returning xml file data

Public class VideoNewsService {/*** get the latest video information */public static List
  
   
GetLastNews () throws Exception {String path = "request servlet url"; URL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); if (conn. getResponseCode () = 200) {InputStream inStream = conn. getInputStream (); return parseXML (inStream);} return null;}/*** parse the xml data returned by the server
    
     
    
        
     
      
90
        
     
    
        
     
      
20
        
     
    
        
     
      
30
        
    
   */Private static List
   
    
ParseXML (InputStream inStream) throws Exception {List
    
     
Newses = new ArrayList
     
      
(); News news = null; XmlPullParser parser = Xml. newPullParser (); parser. setInput (inStream, "UTF-8"); int event = parser. getEventType (); // judge the event of the xml Parser while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_TAG: if ("news ". equals (parser. getName () {int id = new Integer (parser. getAttributeValue (0); news = new News (); news. setId (id);} else if ("title ". equals (parser. getName () {news. setTitle (parser. nextText ();} else if ("timelength ". equals (parser. getName () {news. setTimelength (new Integer (parser. nextText ();} break; case XmlPullParser. END_TAG: if ("news ". equals (parser. getName () {newses. add (news); news = null;} break;} event = parser. next () ;}return newses ;}}
     
    
   
  

(6) use it in the Mainactivity. java file (set a value for a listview)

Public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); ListView listView = (ListView) this. findViewById (R. id. listView); try {List
  
   
Videos = VideoNewsService. getLastNews (); // you need to change it to your local Http Request Path List.
   
    
> Data = new ArrayList
    
     
> (); For (News news: videos) {HashMap
     
      
Item = new HashMap
      
        (); Item. put ("id", news. getId (); item. put ("title", news. getTitle (); item. put ("timelength", getResources (). getString (R. string. timelength) + news. getTimelength () + getResources (). getString (R. string. min); data. add (item);} SimpleAdapter adapter = new SimpleAdapter (this, data, R. layout. item, new String [] {"title", "timelength"}, new int [] {R. id. title, R. id. timelength}); listView. setAdapter (adapter);} catch (Exception e) {e. printStackTrace ();}}}
      
     
    
   
  

This completes the operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.