實驗步驟:
1、 配置J2EE開發環境,並部署web應用viderweb,啓動服務
2、 打開瀏覽器訪問網址
http://localhost:8080/videoweb/video/list.do
演示xml數據
3、 創建android客戶端工程,實現訪問上面的網址並且將取得的xml數據進行解析並顯示,如下圖:
Android客戶端代碼
程序流程
資源
<resources>
<string name="app_name">視頻資訊客戶端</string>
<string name="error">網絡連接失敗</string>
</resources>
佈局
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="250dip"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timelength"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
item.xml
添加業務bean類Video
添加業務類VideoService類
public class VideoService {
/**
* 從服務器獲取最新的視頻資訊
*
* @return
* @throws Throwable
*/
public static List<Video> getLastVideos() throws Throwable {
String path =
"http://192.168.1.102:8080/videoweb/video/list.do";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
return parseXML(inStream);
}
private static List<Video> parseXML(InputStream inStream) throws Exception {
Video video = null;
List<Video> videos = null;
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(inStream, "UTF-8");
int event = pullParser.getEventType();// 觸發第一個事件
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
videos = new ArrayList<Video>();
break;
case XmlPullParser.START_TAG:
if ("video".equals(pullParser.getName())) {
int id = new Integer(pullParser.getAttributeValue(0));
video = new Video();
video.setId(id);
}
if (video != null) {
if ("title".equals(pullParser.getName())) {
video.setTitle(pullParser.nextText());
}
if ("timelength".equals(pullParser.getName())) {
video.setTimelength(new Integer(pullParser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if ("video".equals(pullParser.getName())) {
videos.add(video);
video = null;
}
break;
}
event = pullParser.next();
}
return videos;
}
}
Activity類MyVideoNewsActivity
public class MyVideoNewsActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
List<Video> videos = VideoService.getLastVideos();//獲取最新視頻資訊
ListView listView = (ListView)this.findViewById(R.id.listView);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(Video video : videos){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("title", video.getTitle());
item.put("timelength", "時長:"+video.getTimelength());
item.put("id", video.getId());
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 (Throwable e) {
Log.e("TAG", e.toString());
Toast.makeText(this, R.string.error, 1).show();
}
}
}