(1) Create entity class News
publicclass News { private Integer id; private String title; private Integer timelength; publicNews(Integer id, String title, Integer timelength) { this.id = id; this.title = title; this.timelength = timelength; } //get、set方法}
(2) An interface-oriented implementation class created by interface programming:
publicinterface VideoNewsService { /** * 获取最新的视频资讯 * @return */ publicgetLastNews();}
The implementation class of the interface for adding some false data
Public class Videonewsservicebean implements videonewsservice { PublicList<news>getlastnews() {List<news> Newes =NewArraylist<news> (); Newes.add (NewNews ( -,"Pleasant goat and grey wolf complete", +)); Newes.add (NewNews (Ten,"Real shot ship helicopters in East China Sea Rescue Drill", -)); Newes.add (NewNews ( About,"Cameroon vs Holland", -));returnNewes; }}
(3) Return data large client via servlet
Public class listservlet extends httpservlet { Private Static Final LongSerialversionuid =1LPrivateVideonewsservice Service =NewVideonewsservicebean ();protected void Doget(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {doPost (request, response); }protected void DoPost(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {list<news> videos = service.getlastnews (); Request.setattribute ("Videos", videos);//redirect a JSP interface to return the JSP page data when requested by the clientRequest.getrequestdispatcher ("/web-inf/page/videonews.jsp"). Forward (request, response); }}
(4) Generate the JSP page of the XML file (returned here as an XML file so 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"%><?xml version= "1.0" encoding= "UTF-8"?><videonews><c:foreach Items="${videos}" var="video"> <news id="${video.id}"> <title>${video.title}</title> <timelength>${video.timelength}</timelength> </News> </C:foreach></videonews>
(5) using an XML file in the client
The same client also needs a news entity class, omitted here
Create a service class for parsing XML and returning XML file data
Public class videonewsservice { /** * Get the latest video information * * Public StaticList<news>getlastnews()throwsexception{String Path ="Request URL for servlet"; URL url =NewURL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setconnecttimeout ( the); Conn.setrequestmethod ("GET");if(Conn.getresponsecode () = = $) {InputStream instream = Conn.getinputstream ();returnParsexml (instream); }return NULL; }/** * Parse the XML data returned by the server <?xml version= "1.0" encoding= "UTF-8"?> <videonews> <news id= "a" > <titl E> sheep and grey Wolf complete </title> <timelength>90</timelength> </news> <news id= "a" > <title& gt; Lao Zhang with Grey Wolf </title> <timelength>20</timelength> </news> <news id= "" "> <title> Lao Fang and lili</title> <timelength>30</timelength> </news></videonews> * * Private StaticList<news>Parsexml(InputStream instream)throwsException {list<news> newses =NewArraylist<news> (); News news =NULL; Xmlpullparser parser = Xml.newpullparser (); Parser.setinput (Instream,"UTF-8");intevent = Parser.geteventtype ();//To determine the events of the XML parser while(Event! = xmlpullparser.end_document) {Switch(event) { CaseXmlpullparser.start_tag:if("News". Equals (Parser.getname ())) {intID =NewInteger (Parser.getattributevalue (0)); News =NewNews (); News.setid (ID); }Else if("title". Equals (Parser.getname ())) {News.settitle (Parser.nexttext ()); }Else if("Timelength". Equals (Parser.getname ())) {News.settimelength (NewInteger (Parser.nexttext ())); } Break; CaseXmlpullparser.end_tag:if("News". Equals (Parser.getname ())) {Newses.add (news); News =NULL; } Break; } event = Parser.next (); }returnnewses; }}
(6) used in the Mainactivity.java file (here is a ListView setting value)
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<news> videos = videonewsservice.getlastnews ();//need to modify the HTTP request path to your native computerlistNewArraylist for(News News:videos) {hashmap<string, object> item =NewHashmap<string, object> (); 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 =NewSimpleadapter ( This, data, R.layout.item,Newstring[]{"title","Timelength"},New int[]{r.id.title, r.id.timelength}]; Listview.setadapter (adapter); }Catch(Exception e) {E.printstacktrace (); } }}
To complete this operation
Convert Android data to XML for clients and clients using XML data