Android获取网络XML/JSON数据


Video.java

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

StreamTool.java

  1. package cn.itcast.utils;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5.   
  6. public class StreamTool {  
  7.   
  8.     /** 
  9.      * 从输入流中获取数据 
  10.      * @param inStream 输入流 
  11.      * @return 
  12.      * @throws Exception 
  13.      */  
  14.     public static byte[] readInputStream(InputStream inStream) throws Exception{  
  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.         inStream.close();  
  22.         return outStream.toByteArray();  
  23.     }  
  24. }  

VideoService.java

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

MainActivity.java

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

相关内容