您的位置:首页 > 移动开发 > Android开发

Android开发之获取web服务器xml数据

2011-12-22 19:39 471 查看
Android开发之获取web服务器xml数据

首先我们需要配置J2EE开发环境,并部署web应用viderweb,启动服务

然后开始我们的程序代码

我们的程序大致是获取web服务器xmlàpull解析xmlàListView列表显示数据

添加业务bean类Video

package cn.class3g.domain;

public class Video {

private int id;

private String title;

private int timeLenght;

public Video() {

super();

// TODO Auto-generated constructor stub

}

public Video(int id, String title, int timeLenght) {

super();

this.id = id;

this.title = title;

this.timeLenght = timeLenght;

}

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 getTimeLenght() {

return timeLenght;

}

public void setTimeLenght(int timeLenght) {

this.timeLenght = timeLenght;

}

@Override

public String toString() {

return "Video [id=" + id + ", title=" + title + ", timeLenght="

+ timeLenght + "]";

}

}


布局

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/listViewID"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

</ListView>

</LinearLayout>


Item.xml

<?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/titleID"

android:layout_width="200dp"

android:layout_height="wrap_content" />

<TextView

android:id="@+id/timelengthID"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

</LinearLayout>


添加业务类VideoService类

package cn.class3g.service;

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 cn.class3g.domain.Video;

public class VideoService {

public static List<Video> getLastVideos() throws Exception {

String path = "http://192.168.65.2:8080/videoweb/video/list.do";

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

conn.setRequestMethod("GET");

InputStream inStream = conn.getInputStream();

return parseXML(inStream);

}

private static List<Video> parseXML(InputStream inStream) throws Exception {

XmlPullParser parser = Xml.newPullParser();

parser.setInput(inStream, "UTF-8");

int event = parser.getEventType();

List<Video> videos = null;

Video video = null;

while (event != XmlPullParser.END_DOCUMENT) {

switch (event) {

case XmlPullParser.START_DOCUMENT:

videos = new ArrayList<Video>();

break;

case XmlPullParser.START_TAG:

if ("video".equals(parser.getName())) {

video = new Video();

int id = Integer.valueOf(parser.getAttributeValue(0));

video.setId(id);

} else if ("title".equals(parser.getName()) && video != null) {

video.setTitle(parser.nextText());

} else if ("timelength".equals(parser.getName())

&& video != null) {

video.setTimeLenght(Integer.valueOf(parser.nextText()));

}

break;

case XmlPullParser.END_TAG:

if ("video".equals(parser.getName()) && video != null) {

videos.add(video);

video = null;

}

break;

}

event = parser.next();

}

return videos;

}

}


VideoListActivity

package cn.class3g.video;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import cn.class3g.domain.Video;

import cn.class3g.service.VideoService;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

public class VideoListActivity extends Activity {

ListView videolv;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

try {

findViews();

} catch (Exception e) {

// TODO Auto-generated catch block

Log.e("tag", e.toString());

// Toast.makeText(this, text, duration)

}

}

private void findViews() throws Exception {

videolv = (ListView) this.findViewById(R.id.listViewID);

ArrayList<Video> videos = (ArrayList<Video>) VideoService

.getLastVideos();

List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

for (Video v : videos) {

HashMap<String, Object> item = new HashMap<String, Object>();

item.put("title", v.getTitle());

item.put("timelenght", "时长“:" + v.getTimeLenght() + "分钟");

item.put("id", v.getId());

data.add(item);

}

SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,

new String[] { "title", "timelenght" }, new int[] {

R.id.titleID, R.id.timelengthID });

videolv.setAdapter(adapter);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: