歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android獲取網絡XML/JSON數據

Android獲取網絡XML/JSON數據

日期:2017/3/1 11:12:59   编辑:Linux編程

Video.java

  1. package cn.itcast.domain;
  2. public class Video {
  3. private Integer id;
  4. private String title;
  5. private Integer time;
  6. public Video(){}
  7. public Video(Integer id, String title, Integer time) {
  8. this.id = id;
  9. this.title = title;
  10. this.time = time;
  11. }
  12. public Integer getId() {
  13. return id;
  14. }
  15. public void setId(Integer id) {
  16. this.id = id;
  17. }
  18. public String getTitle() {
  19. return title;
  20. }
  21. public void setTitle(String title) {
  22. this.title = title;
  23. }
  24. public Integer getTime() {
  25. return time;
  26. }
  27. public void setTime(Integer time) {
  28. this.time = time;
  29. }
  30. }

StreamTool.java

  1. package cn.itcast.utils;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream;
  4. public class StreamTool {
  5. /**
  6. * 從輸入流中獲取數據
  7. * @param inStream 輸入流
  8. * @return
  9. * @throws Exception
  10. */
  11. public static byte[] readInputStream(InputStream inStream) throws Exception{
  12. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  13. byte[] buffer = new byte[1024];
  14. int len = 0;
  15. while( (len=inStream.read(buffer)) != -1 ){
  16. outStream.write(buffer, 0, len);
  17. }
  18. inStream.close();
  19. return outStream.toByteArray();
  20. }
  21. }

VideoService.java

  1. package cn.itcast.service;
  2. import java.io.InputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9. import org.xmlpull.v1.XmlPullParser;
  10. import Android.util.Xml;
  11. import cn.itcast.domain.Video;
  12. import cn.itcast.utils.StreamTool;
  13. public class VideoService {
  14. /**
  15. * 獲取最新的視頻資訊
  16. * @return
  17. * @throws Exception
  18. */
  19. public static List<Video> getLastVideos() throws Exception{
  20. String path = "http://192.168.1.100:8080/videoweb/video/list.do";
  21. URL url = new URL(path);
  22. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  23. conn.setReadTimeout(5*1000);
  24. conn.setRequestMethod("GET");
  25. InputStream inStream = conn.getInputStream();
  26. return parseXML(inStream);
  27. }
  28. public static List<Video> getJSONLastVideos() throws Exception{
  29. List<Video> videos = new ArrayList<Video>();
  30. String path = "http://192.168.1.100:8080/videoweb/video/list.do?format=json";
  31. URL url = new URL(path);
  32. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  33. conn.setReadTimeout(5*1000);
  34. conn.setRequestMethod("GET");
  35. InputStream inStream = conn.getInputStream();
  36. byte[] data = StreamTool.readInputStream(inStream);
  37. String json = new String(data);
  38. JSONArray array = new JSONArray(json);
  39. for(int i=0 ; i < array.length() ; i++){
  40. JSONObject item = array.getJSONObject(i);
  41. int id = item.getInt("id");
  42. String title = item.getString("title");
  43. int timelength = item.getInt("timelength");
  44. videos.add(new Video(id, title, timelength));
  45. }
  46. return videos;
  47. }
  48. /**
  49. * 解析服務器返回的協議,得到視頻資訊
  50. * @param inStream
  51. * @return
  52. * @throws Exception
  53. */
  54. private static List<Video> parseXML(InputStream inStream) throws Exception{
  55. List<Video> videos = null;
  56. Video video = null;
  57. XmlPullParser parser = Xml.newPullParser();
  58. parser.setInput(inStream, "UTF-8");
  59. int eventType = parser.getEventType();//產生第一個事件
  60. while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文檔結束事件
  61. switch (eventType) {
  62. case XmlPullParser.START_DOCUMENT:
  63. videos = new ArrayList<Video>();
  64. break;
  65. case XmlPullParser.START_TAG:
  66. String name = parser.getName();//獲取解析器當前指向的元素的名稱
  67. if("video".equals(name)){
  68. video = new Video();
  69. video.setId(new Integer(parser.getAttributeValue(0)));
  70. }
  71. if(video!=null){
  72. if("title".equals(name)){
  73. video.setTitle(parser.nextText());//獲取解析器當前指向元素的下一個文本節點的值
  74. }
  75. if("timelength".equals(name)){
  76. video.setTime(new Integer(parser.nextText()));
  77. }
  78. }
  79. break;
  80. case XmlPullParser.END_TAG:
  81. if("video".equals(parser.getName())){
  82. videos.add(video);
  83. video = null;
  84. }
  85. break;
  86. }
  87. eventType = parser.next();
  88. }
  89. return videos;
  90. }
  91. }

MainActivity.java

  1. package cn.itcast.videoclient;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import cn.itcast.domain.Video;
  6. import cn.itcast.service.VideoService;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. import android.widget.Toast;
  13. public class MainActivity extends Activity {
  14. private ListView listView;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. listView = (ListView)this.findViewById(R.id.listView);
  20. try {
  21. List<Video> videos = VideoService.getJSONLastVideos();
  22. List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
  23. for(Video video : videos){
  24. HashMap<String, Object> item = new HashMap<String, Object>();
  25. item.put("id", video.getId());
  26. item.put("title", video.getTitle());
  27. item.put("timelength", "時長:"+ video.getTime());
  28. data.add(item);
  29. }
  30. SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
  31. new String[]{"title", "timelength"}, new int[]{R.id.title, R.id.timelength});
  32. listView.setAdapter(adapter);
  33. } catch (Exception e) {
  34. Toast.makeText(MainActivity.this, "獲取最新視頻資訊失敗", 1).show();
  35. Log.e("MainActivity", e.toString());
  36. }
  37. }
  38. }
Copyright © Linux教程網 All Rights Reserved