获取web服务器xml数据
16lz
2021-01-24
实验步骤:
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
package cn.class3g.domain; public class Video { private Integer id; private String title; private Integer timelength; public Video(Integer id, String title, Integer timelength) { super(); this.id = id; this.title = title; this.timelength = timelength; } public Video() { } 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; } public String toString() { return "Video [id=" + id + ", title=" + title + ", timelength=" + timelength + "]"; } }
添加业务类VideoService类
publicclassVideoService{
/** * 从服务器获取最新的视频资讯 * * @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(); } } }
更多相关文章
- Android(安卓)ApiDemos示例解析(88):Media->VideoView
- Android(安卓)Asynchronous Http Client-Android异步网络请求客
- [置顶] android 心跳包的分析
- Android(安卓)客户端起HttpServer NanoHttpd
- Android播放视频的方式
- Android客户端向服务器端传值——登录实现(一)
- Android使用FFmpeg动态库播放视频
- Android(安卓)音视频深入 四 Android原生API录视频MP4,有缺陷,没有
- Android(安卓)应用初始化及窗体事件的分发