Android开发17——获取网络资源之XML数据

一、项目背景
在Android开发中有一项非常广泛的应用:Android项目获取另一个web项目的资源或者返回的数据。

本文获取web项目返回的XML数据。

 

 

二、实例代码

 

Web项目

/**
 * 新闻业务类
 * 
 * @author xy
 * 
 */
public class VideoNewsServiceImpl implements VideoNewsService
{
	public List<VideoNews> readNews()
	{
		List<VideoNews> lst = new ArrayList<VideoNews>();
		lst.add(new VideoNews(1, "喜洋洋", 20));
		lst.add(new VideoNews(2, "变形金刚", 10));
		lst.add(new VideoNews(3, "功夫熊猫", 20));
		return lst;
	}
}

/**
 * 新闻Servlet
 * 
 * @author xy
 * 
 */
public class ListServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	private VideoNewsService vs = new VideoNewsServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		List<VideoNews> news = vs.readNews();
		request.setAttribute("xmlnews", news);
		request.getRequestDispatcher("/WEB-INF/pages/newsxml.jsp").forward(request, response);
	}
}
<%@ page language="java" contentType="text/xml; charset=utf-8" pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><?xml version="1.0" encoding="UTF-8"?>
<videoNews>
	<c:forEach items="${xmlnews}" var="n">
		<news id="${n.id }">
			<title>${n.title }</title>
			<length>${n.timeLength }</length>
		</news>
	</c:forEach>
</videoNews>

Android项目

public class VideoNewsServiceImpl implements VideoNewsService
{
	/**
	 * 获取最新视频资讯,从XML文件中,比较常用
	 */
	public List<VideoNews> getNewsFromXML() throws Exception
	{
		List<VideoNews> lst = new ArrayList<VideoNews>();
		String path = "http://xxx.xxx.xxx.xxx:8080/web/ListServlet";
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setReadTimeout(5000);
		conn.setRequestMethod("GET");
		if (200 == conn.getResponseCode())
		{
			InputStream instream = conn.getInputStream();
			lst = parseXML(instream);
		}
		return lst;
	}

	/**
	 * 解析XML
	 */
	private List<VideoNews> parseXML(InputStream instream) throws Exception
	{
		List<VideoNews> lst = null;
		VideoNews vn = null;
		// 利用ANDROID提供的API快速获得pull解析器
		XmlPullParser pullParser = Xml.newPullParser();
		// 设置需要解析的XML数据
		pullParser.setInput(instream, "UTF-8");
		// 取得事件
		int event = pullParser.getEventType();
		// 若为解析到末尾,文档结束
		while (event != XmlPullParser.END_DOCUMENT)
		{
			String nodeName = pullParser.getName();
			switch (event)
			{
				// 文档开始
				case XmlPullParser.START_DOCUMENT:
					lst = new ArrayList<VideoNews>();
					break;
				// 标签开始
				case XmlPullParser.START_TAG:
					if ("news".equals(nodeName))
					{
						vn = new VideoNews();
						Integer id = Integer.valueOf(pullParser.getAttributeValue(0));
						vn.setId(id);
					}
					if ("title".equals(nodeName))
					{
						String title = pullParser.nextText();
						vn.setTitle(title);
					}
					if ("length".equals(nodeName))
					{
						Integer length = Integer.valueOf(pullParser.nextText());
						vn.setTimeLength(length);
					}
					break;
				// 标签结束
				case XmlPullParser.END_TAG:
					if ("news".equals(nodeName))
					{
						lst.add(vn);
						vn = null;
					}
					break;
			}
			// 下一个标签
			event = pullParser.next();
		}
		return lst;
	}
}

最后把得到的数据绑定到ListView中即可。

上一篇:实现简单的CSharpShell -- OrcShell (1) 基本结构


下一篇:【Android游戏开发之六】在SurfaceView中添加组件!!!!并且相互交互数据!!!!