Android开发之获取web服务器xml数据
2011-12-22 19:39
471 查看
Android开发之获取web服务器xml数据
首先我们需要配置J2EE开发环境,并部署web应用viderweb,启动服务
然后开始我们的程序代码
我们的程序大致是获取web服务器xmlàpull解析xmlàListView列表显示数据
添加业务bean类Video
布局
Item.xml
添加业务类VideoService类
VideoListActivity
首先我们需要配置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); } }
相关文章推荐
- Android应用开发之获取web服务器xml数据
- Android应用开发之获取web服务器xml数据
- 获取web服务器xml数据总结
- android开发 如何通过web服务器访问MYSQL数据库并且使其数据同步到android SQLite数据库?
- Android开发17——获取网络资源之XML数据
- Android开发17——获取网络资源之XML数据
- Android开发17——获取网络资源之XML数据
- Android客户端解析web服务器XML数据小问题
- Android之通过向WebService服务器发送XML数据获取相关服务
- android解析xml和json数据(获取本地服务器数据)
- 获取web服务器xml数据
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- Android之通过向WebService服务器发送XML数据获取相关服务
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- Android--通过Http协议向web服务器发送XML数据和调用webService
- android开发案例之使用JSON数据向服务器提交和获取服务器传递的Json数据
- Android之通过向WebService服务器发送XML数据获取相关服务
- Android之通过向WebService服务器发送XML数据获取相关服务