首先我们创建服务端
-
News.java
public class News {
private int id;
private String title;
private Date 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 Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public News(int id, String title, Date time) {
super();
this.id = id;
this.title = title;
this.time = time;
}
public News() {
}
}
-
NewsDaoImpl.java
public class NewsDaoImpl implements NewsDao{
List<News> list = null;
public List<News> getNews() {
list = new ArrayList<News>();
//初始化数据
News n1 = new News(12, "xinwen",new Date(System.currentTimeMillis()) );
News n2 = new News(13, "yule", new Date(System.currentTimeMillis()) );
News n3 = new News(14, "tiyu", new Date(System.currentTimeMillis()) );
//数据存到集合里面去
list.add(n1);
list.add(n2);
list.add(n3);
return list;
}
}
-
xmlServlet.java (servlet)
public class xmlServlet extends HttpServlet {
private NewsDaoImpl newsDaoImpl = new NewsDaoImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取集合东西,显示到jsp界面
List<News> nList = newsDaoImpl.getNews();
//设置显示到jsp界面
request.setAttribute("newslist", nList);
//跳转
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
}
-
show.jsp(创建项目的时候选5.0版本)
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<News>
<c:forEach items="${newslist}" var="news">
<news id="${news.id}">
<title>${news.title}</title>
<timedate>${news.time}</timedate>
</news>
</c:forEach>
</News>
-
启动Tomcat6.0 测试一下
路径: http://localhost:8080/ServerXML/xmlServlet
接下来我们创建客户端(客户端的需求就是:通过HttpURLConnection来访问服务端得到xml文件并经过pull解析器来进行解析转变成集合显示到listview里面进行展示)
-
News.java (注:Date time导的包是util包)
public class News {
private int id;
private String title;
private Date 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 Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", time=" + time + "]";
}
}
-
NewsService.java
public class NewsService {
//得到从服务端解析xml以后的list集合
public static List<News> getListnews(){
List<News> list = new ArrayList<News>();
//这里的path是本机的IP地址
String path = "http://100.77.46.77:8080/ServerXML/xmlServlet";
try {
HttpURLConnection conn = (HttpURLConnection)new URL(path).openConnection();
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
//获取服务端的输入流
InputStream inputxml = conn.getInputStream();
//xml pull解析
list = pullxml(inputxml);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//经典的pull解析方法
public static List<News> pullxml(InputStream is) throws Exception{
List<News> xmlList = null;
News news = null;
XmlPullParser parse = Xml.newPullParser();
parse.setInput(is, "utf-8");
//产生第一个事件,就像游标一样
int eventype=parse.getEventType();
while(eventype!=XmlPullParser.END_DOCUMENT){
switch(eventype)
{
//判断当前是不是文档开始的事件
case XmlPullParser.START_DOCUMENT:
xmlList = new ArrayList<News>();//初始化存放student的集合
break;
//判断当前事件是不是标签元素的开始事件
case XmlPullParser.START_TAG:
String name=parse.getName();//获取当前解析器指向的元素名称
if(name.equals("news"))
{
news=new News();
news.setId(new Integer(parse.getAttributeValue(0)));
}
if(news!=null)
{
if(name.equals("title"))
{
news.setTitle(parse.nextText());//获取解析器当前指向元素的下一个文本节点的值
}
if(name.equals("timedate"))
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = new String(parse.nextText());
news.setTime(sdf.parse(date));
}
}
break;
// 判断当前事件是否为标签元素结束事件
case XmlPullParser.END_TAG :
if(parse.getName().equals("news"))// 判断结束标签元素是否是Student
{
xmlList.add(news);
news=null;
}
break;
}
// 进入下一个元素并触发相应事件
eventype=parse.next();
//测试xmlList是否有值
Log.e("============", xmlList.toString());
}
return xmlList;
}
}
-
MainActivity.java(主布局)
/**
* 安卓开发之通过Android客户端从网络获取xml文件并解析显示
* @author dandan
*
*/
public class MainActivity extends Activity {
private ListView lv;
List<Map<String, Object>> data =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iviview();
//这里通过线程来执行,去掉线程也是可以的,,,
new Thread(new Runnable() {
@Override
public void run() {
data = getData();
if (data != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data,
R.layout.text, new String[] {"id","title","time"}, new int[]{R.id.id,R.id.title,R.id.time});
lv.setAdapter(adapter);
}
});
}
}
}).start();
}
//初始化控件
private void iviview() {
lv = (ListView) findViewById(R.id.lv);
}
//得到处理后的数据
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
List<News> newlist = NewsService.getListnews();
for(News n:newlist){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", n.getId());
map.put("title", n.getTitle());
map.put("time", n.getTime());
list.add(map);
}
return list;
}
}
-
注:联网权限不要忘记加!!!
<uses-permission android:name="android.permission.INTERNET"/>
-
演示: