首先建立一个简单的web工程,使用servlet技术:
下面是servlet的实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/**
* @FILE:ListServlet.java
* @AUTHOR:Administrator
* @DATE:2013-5-19 下午6:03:19
**/
package
com.yehui.servlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
java.util.List;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
com.yehui.service.VideoNewsService;
import
com.yehui.service.bean.News;
import
com.yehui.service.impl.VideoNewsServiceImpl;
/*******************************************
*
* @CLASS:ListServlet
* @DESCRIPTION:
* @AUTHOR:Administrator
* @VERSION:v1.0
* @DATE:2013-5-19 下午6:03:19
*******************************************/
public
class
ListServlet
extends
HttpServlet {
private
VideoNewsService service =
new
VideoNewsServiceImpl();
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
List<News> videos = service.getLastNews();
request.setAttribute(
"videos"
, videos);
// ("")里面是jsp的文件路径
request.getRequestDispatcher(
"/WEB-INF/page/videonews.jsp"
).forward(request, response);
}
}
|
web的service操作的bean:News:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
/**
* @FILE:News.java
* @AUTHOR:Administrator
* @DATE:2013-5-19 下午6:08:00
**/
package
com.yehui.service.bean;
/*******************************************
*
* @CLASS:News
* @DESCRIPTION:
* @AUTHOR:Administrator
* @VERSION:v1.0
* @DATE:2013-5-19 下午6:08:00
*******************************************/
public
class
News {
private
Integer id;
private
String title;
private
Integer timelength;
public
News() {
}
public
News(Integer id, String title, Integer timelength) {
this
.id = id;
this
.title = title;
this
.timelength = timelength;
}
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id = id;
}
public
String getTitle() {
return
title;
}
public
void
setTitle(String title) {
this
.title = title;
}
public
Integer getTimelength() {
return
timelength;
}
public
void
setTimelength(Integer timelength) {
this
.timelength = timelength;
}
}
|
web的service接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
/**
* @FILE:VideoNewsService.java
* @AUTHOR:Administrator
* @DATE:2013-5-19 下午6:14:18
**/
package
com.yehui.service;
import
java.util.List;
import
com.yehui.service.bean.News;
/*******************************************
*
* @CLASS:VideoNewsService
* @DESCRIPTION:
* @AUTHOR:Administrator
* @VERSION:v1.0
* @DATE:2013-5-19 下午6:14:18
*******************************************/
public
interface
VideoNewsService {
/**@description:获取最新的视频资讯
* @author:Administrator
* @return:List<News>
*/
public
List<News> getLastNews();
}
|
service的实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* @FILE:VideoNewsServiceImpl.java
* @AUTHOR:Administrator
* @DATE:2013-5-19 下午6:06:33
**/
package
com.yehui.service.impl;
import
java.util.ArrayList;
import
java.util.List;
import
com.yehui.service.VideoNewsService;
import
com.yehui.service.bean.News;
/*******************************************
*
* @CLASS:VideoNewsServiceImpl
* @DESCRIPTION:
* @AUTHOR:Administrator
* @VERSION:v1.0
* @DATE:2013-5-19 下午6:06:33
*******************************************/
public
class
VideoNewsServiceImpl
implements
VideoNewsService {
public
List<News> getLastNews(){
List<News> newes=
new
ArrayList<News>();
newes.add(
new
News(
1
,
"喜洋洋"
,
90
));
newes.add(
new
News(
2
,
"灰太狼"
,
30
));
newes.add(
new
News(
3
,
"泷泽萝拉"
,
10
));
return
newes;
}
}
|
jsp文件,这里注意是要返回xml的结果:所以文件的第一行必须加上<?xml version="1.0" encoding="UTF-8"?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* @FILE:VideoNewsServiceImpl.java
* @AUTHOR:Administrator
* @DATE:2013-5-19 下午6:06:33
**/
package
com.yehui.service.impl;
import
java.util.ArrayList;
import
java.util.List;
import
com.yehui.service.VideoNewsService;
import
com.yehui.service.bean.News;
/*******************************************
*
* @CLASS:VideoNewsServiceImpl
* @DESCRIPTION:
* @AUTHOR:Administrator
* @VERSION:v1.0
* @DATE:2013-5-19 下午6:06:33
*******************************************/
public
class
VideoNewsServiceImpl
implements
VideoNewsService {
public
List<News> getLastNews(){
List<News> newes=
new
ArrayList<News>();
newes.add(
new
News(
1
,
"喜洋洋"
,
90
));
newes.add(
new
News(
2
,
"灰太狼"
,
30
));
newes.add(
new
News(
3
,
"泷泽萝拉"
,
10
));
return
newes;
}
}
|
jsp文件,这里注意是要返回xml的结果:所以文件的第一行必须加上<?xml version="1.0" encoding="UTF-8"?>
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language=
"java"
contentType=
"text/xml; charset=UTF-8"
import
=
"java.util.*"
pageEncoding=
"utf-8"
%><?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>
|
web.xml文件配置servlet:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="3.0"
-
xmlns="http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
-
<display-name></display-name>
-
<servlet>
-
<description>This is the description of my J2EE component</description>
-
<display-name>This is the display name of my J2EE component</display-name>
-
<servlet-name>ListServlet</servlet-name>
-
<servlet-class>com.yehui.servlet.ListServlet</servlet-class>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>ListServlet</servlet-name>
-
<url-pattern>/ListServlet</url-pattern>
-
</servlet-mapping>
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
</web-app>
依然为service操作的bean:
-
/**
-
* @FILE:News.java
-
* @AUTHOR:Administrator
-
* @DATE:2013-5-19 下午6:08:00
-
**/
-
package com.yehui.bean;
-
-
/*******************************************
-
*
-
* @CLASS:News
-
* @DESCRIPTION:
-
* @AUTHOR:Administrator
-
* @VERSION:v1.0
-
* @DATE:2013-5-19 下午6:08:00
-
*******************************************/
-
public class News {
-
private Integer id;
-
private String title;
-
private Integer timelength;
-
-
public News() {
-
}
-
-
public News(Integer id, String title, Integer timelength) {
-
this.id = id;
-
this.title = title;
-
this.timelength = timelength;
-
}
-
-
public Integer getId() {
-
return id;
-
}
-
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
-
public String getTitle() {
-
return title;
-
}
-
-
public void setTitle(String title) {
-
this.title = title;
-
}
-
-
public Integer getTimelength() {
-
return timelength;
-
}
-
-
public void setTimelength(Integer timelength) {
-
this.timelength = timelength;
-
}
-
-
}
-
/**
-
* @FILE:VideoNewsService.java
-
* @AUTHOR:Administrator
-
* @DATE:2013-5-19 下午8:29:22
-
**/
-
package com.yehui.service;
-
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.xmlpull.v1.XmlPullParser;
-
-
-
-
import android.util.Xml;
-
-
import com.yehui.bean.News;
-
-
/*******************************************
-
*
-
* @CLASS:VideoNewsService
-
* @DESCRIPTION:
-
* @AUTHOR:Administrator
-
* @VERSION:v1.0
-
* @DATE:2013-5-19 下午8:29:22
-
*******************************************/
-
public class VideoNewsService {
-
public static List<News> getLastNews() throws Exception {
-
List<News> newes = new ArrayList<News>();
-
URL url = new URL("http://169.254.161.54:8888/web/ListServlet");
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(5000);
-
conn.connect();
-
if (conn.getResponseCode() == 200) {
-
InputStream inputStream = conn.getInputStream();
-
// 因为页面返回值是xml文件,所以现在就要解析这个xml,使用pull解析器解析
-
-
newes= parseXML(inputStream);
-
-
}
-
return newes;
-
}
-
-
/**
-
* @description:pull解析服务器返回xml文件
-
* @author:Administrator
-
* @return:List<News>
-
* @param inputStream
-
* @return
-
* @throws XmlPullParserException
-
* @throws IOException
-
*/
-
-
private static List<News> parseXML(InputStream inputStream)
-
throws Exception {
-
XmlPullParser xmlPullParser = Xml.newPullParser();
-
xmlPullParser.setInput(inputStream, "utf-8");
-
int event = xmlPullParser.getEventType();
-
List<News> newes = new ArrayList<News>();
-
News news = null;
-
while (event != xmlPullParser.END_DOCUMENT) {
-
switch (event) {
-
case 2:
-
if ("news".equals(xmlPullParser.getName())) {
-
int id = Integer
-
.valueOf(xmlPullParser.getAttributeValue(0));
-
news = new News();
-
news.setId(id);
-
} else if ("title".equals(xmlPullParser.getName())) {
-
news.setTitle(xmlPullParser.nextText());
-
} else if ("timelength".equals(xmlPullParser.getName())) {
-
news.setTimelength(Integer.valueOf(xmlPullParser.nextText()));
-
}
-
break;
-
case 3:
-
if ("news".equals(xmlPullParser.getName())) {
-
newes.add(news);
-
news = null;
-
}
-
break;
-
default:
-
break;
-
-
}
-
-
event = xmlPullParser.next();
-
}
-
return newes;
-
}
-
}
-
package com.yehui.news;
-
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import com.yehui.bean.News;
-
import com.yehui.service.VideoNewsService;
-
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.app.Activity;
-
import android.view.Menu;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
ListView listview;
-
private Handler handler = new Handler() {
-
-
-
@Override
-
public void handleMessage(Message msg) {
-
try {
-
List<News> newes = (List<News>) msg.obj;
-
-
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
-
for (News news : newes) {
-
HashMap<String, Object> item = new HashMap<String, Object>();
-
item.put("title", news.getTitle());
-
item.put("timelength", news.getTimelength());
-
data.add(item);
-
}
-
SimpleAdapter simpleAdapter = new SimpleAdapter(
-
getApplicationContext(), data, R.layout.item,
-
new String[] { "title", "timelength" }, new int[] {
-
R.id.title, R.id.timelength });
-
listview.setAdapter(simpleAdapter);
-
} catch (Exception e) {
-
e.printStackTrace();
-
Toast.makeText(getApplicationContext(), R.string.error, 1)
-
.show();
-
}
-
}
-
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
listview = (ListView) this.findViewById(R.id.listview);
-
new Thread() {
-
-
@Override
-
public void run() {
-
List<News> newes;
-
try {
-
newes = VideoNewsService.getLastNews();
-
Message msg = new Message();
-
msg.obj = newes;
-
handler.sendMessage(msg);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
}
-
-
}.start();
-
-
}
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
-
}
listview布局文件:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
android:paddingBottom="@dimen/activity_vertical_margin"
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
tools:context=".MainActivity" >
-
-
<ListView
-
android:id="@+id/listview"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" >
-
</ListView>
-
-
</LinearLayout>
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="horizontal" >
-
-
<TextView
-
android:id="@+id/title"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content" />
-
-
<TextView
-
android:id="@+id/timelength"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content" />
-
-
</LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>