public class XmlActivity extends Activity { private ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) this.findViewById(R.id.listView); try { List<Video> videos = VideoService.getLastVides(); List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (Video video : videos) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("id", video.getId()); item.put("title", video.getTitle()); item.put("timelength", video.getTime()); 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) { Toast.makeText(XmlActivity.this, "获取最新资讯失败", 1); Log.e("XmlActivity", e.toString()); } } }
public class VideoService { public static void save(List<Person> persons, OutputStream outStream) throws Throwable{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(outStream,"UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for(Person person:persons){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(person.getId())); serializer.startTag(null, "name"); serializer.text(person.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(String.valueOf(person.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); outStream.close(); } public static List<Video> getLastVides() throws Exception{ String path = "http://www.secn.com.cn"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5*1000); conn.setRequestMethod("GET"); InputStream inStream = conn.getInputStream(); return ParserXML(inStream); } private static List<Video> ParserXML(InputStream inStream) throws XmlPullParserException, IOException { List<Video> videos = null; Video video = null; XmlPullParser parser = Xml.newPullParser(); parser.setInput(inStream, "UTF-8"); int eventType = parser.getEventType();//产生第一个事件 while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件 switch (eventType) { case XmlPullParser.START_DOCUMENT: videos = new ArrayList<Video>(); break; case XmlPullParser.START_TAG: String name = parser.getName();//获取解析器当前指向的元素的名称 if("video".equals(name)){ video = new Video(); video.setId(new Integer(parser.getAttributeValue(0))); } if(video!=null){ if("title".equals(name)){ video.setTitle(parser.nextText());//获取解析器当前指向元素的下一个文本节点的值 } if("timelength".equals(name)){ video.setTime(new Integer(parser.nextText())); } } break; case XmlPullParser.END_TAG: if("video".equals(parser.getName())){ videos.add(video); video = null; } break; } eventType = parser.next(); } return videos; } }
public class Video { private int id; private String title; private int time; public Video() {} public Video(int id, String title, int time) { this.id = id; this.title = title; this.time = time; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } }
访问互联网权限:<uses-permission android:name="android.permission.INTERNET" />