歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之獲取web服務器xml數據

Android應用開發之獲取web服務器xml數據

日期:2017/3/1 10:44:30   编辑:Linux編程

實驗步驟:

1、 配置J2EE開發環境,並部署web應用viderweb,啟動服務

2、 打開浏覽器訪問網址

http://localhost:8080/videoweb/video/list.do

演示xml數據

3、 創建Android客戶端工程,實現訪問上面的網址並且將取得的xml數據進行解析並顯示,如下圖:


Android客戶端代碼

程序流程

資源

  1. <resources>
  2. <string name="app_name">視頻資訊客戶端</string>
  3. <string name="error">網絡連接失敗</string>
  4. </resources>

布局

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <ListView
  7. android:id="@+id/listView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" />
  10. </LinearLayout>

item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal" >
  6. <TextView
  7. android:id="@+id/title"
  8. android:layout_width="250dip"
  9. android:layout_height="wrap_content" />
  10. <TextView
  11. android:id="@+id/timelength"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content" />
  14. </LinearLayout>

添加業務bean類Video

  1. package cn.class3g.domain;
  2. public class Video {
  3. private Integer id;
  4. private String title;
  5. private Integer timelength;
  6. public Video(Integer id, String title, Integer timelength) {
  7. super();
  8. this.id = id;
  9. this.title = title;
  10. this.timelength = timelength;
  11. }
  12. public Video() { }
  13. public Integer getId() {
  14. return id;
  15. }
  16. public void setId(Integer id) {
  17. this.id = id;
  18. }
  19. public String getTitle() {
  20. return title;
  21. }
  22. public void setTitle(String title) {
  23. this.title = title;
  24. }
  25. public Integer getTimelength() {
  26. return timelength;
  27. }
  28. public void setTimelength(Integer timelength) {
  29. this.timelength = timelength;
  30. }
  31. public String toString() {
  32. return "Video [id=" + id + ", title=" + title + ", timelength="
  33. + timelength + "]";
  34. }
  35. }

添加業務類VideoService類

  1. package cn.class3g.service;
  2. ...
  3. public class VideoService {
  4. /**
  5. * 從服務器獲取最新的視頻資訊
  6. *
  7. * @return
  8. * @throws Throwable
  9. */
  10. public static List<Video> getLastVideos() throws Throwable {
  11. String path =
  12. "http://192.168.1.102:8080/videoweb/video/list.do";//
  13. 實驗環境中使用pc的ip,不能用localhost或127.0.0.1
  14. URL url = new URL(path);
  15. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  16. conn.setConnectTimeout(5 * 1000);
  17. conn.setRequestMethod("GET");
  18. InputStream inStream = conn.getInputStream();
  19. return parseXML(inStream);
  20. }
  21. private static List<Video> parseXML(InputStream inStream) throws Exception {
  22. Video video = null;
  23. List<Video> videos = null;
  24. XmlPullParser pullParser = Xml.newPullParser();
  25. pullParser.setInput(inStream, "UTF-8");
  26. int event = pullParser.getEventType();// 觸發第一個事件
  27. while (event != XmlPullParser.END_DOCUMENT) {
  28. switch (event) {
  29. case XmlPullParser.START_DOCUMENT:
  30. videos = new ArrayList<Video>();
  31. break;
  32. case XmlPullParser.START_TAG:
  33. if ("video".equals(pullParser.getName())) {
  34. int id = new Integer(pullParser.getAttributeValue(0));
  35. video = new Video();
  36. video.setId(id);
  37. }
  38. if (video != null) {
  39. if ("title".equals(pullParser.getName())) {
  40. video.setTitle(pullParser.nextText());
  41. }
  42. if ("timelength".equals(pullParser.getName())) {
  43. video.setTimelength(new Integer(pullParser.nextText()));
  44. }
  45. }
  46. break;
  47. case XmlPullParser.END_TAG:
  48. if ("video".equals(pullParser.getName())) {
  49. videos.add(video);
  50. video = null;
  51. }
  52. break;
  53. }
  54. event = pullParser.next();
  55. }
  56. return videos;
  57. }
  58. }
Activity類MyVideoNewsActivity
  1. public class MyVideoNewsActivity extends Activity {
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. try {
  6. List<Video> videos = VideoService.getLastVideos();//獲取最新視頻資訊
  7. ListView listView = (ListView)this.findViewById(R.id.listView);
  8. List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
  9. for(Video video : videos){
  10. HashMap<String, Object> item = new HashMap<String, Object>();
  11. item.put("title", video.getTitle());
  12. item.put("timelength", "時長:"+video.getTimelength());
  13. item.put("id", video.getId());
  14. data.add(item);
  15. }
  16. SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
  17. new String[]{"title","timelength"}, new int[]{R.id.title, R.id.timelength});
  18. listView.setAdapter(adapter);
  19. } catch (Throwable e) {
  20. Log.e("TAG", e.toString());
  21. Toast.makeText(this, R.string.error, 1).show();
  22. }
  23. }
  24. }
Androd中獲取網頁代碼

布局

  1. <Button
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:text="@string/btn_text"
  5. android:id="@+id/showBtn"
  6. />
  7. <ImageView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:id="@+id/imageView"
  11. />
代碼:在前面的ImageService.getImage()方法基礎上修改即可

HtmlService

  1. public class HtmlService {
  2. /**
  3. * 獲取給定路徑的html代碼
  4. * @param path 網頁路徑
  5. * @return
  6. * @throws Exception
  7. */
  8. public static String getHtml(String path) throws Exception{
  9. URL url = new URL(path);
  10. //get //post
  11. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  12. conn.setRequestMethod("GET");
  13. conn.setConnectTimeout(5*1000);
  14. InputStream inStream = conn.getInputStream();
  15. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  16. byte[] buffer = new byte[1024];
  17. int len = 0;
  18. while( (len = inStream.read(buffer)) !=-1 ){
  19. outStream.write(buffer, 0, len);
  20. }
  21. byte[] data = outStream.toByteArray();//網頁的二進制數據
  22. outStream.close();
  23. inStream.close();
  24. return new String(data, "gb2312");
  25. }
  26. }
ShowHtmlActivity
  1. public void onClick(View v) {
  2. String path = pathText.getText().toString();
  3. try {
  4. String htmlcode = HtmlService.getHtml(path);
  5. resultView.setText(htmlcode);
  6. } catch (Exception e) {
  7. Log.e(TAG, e.toString());
  8. Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show();
  9. }
  10. }
Copyright © Linux教程網 All Rights Reserved