android从网络获取xml和json数据

转载自:传智播客教程

package cn.itcast.service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import cn.itcast.domain.Video;
import cn.itcast.utils.StreamTool;

public class VideoService {
    /**
     * 从服务端获取xml格式的数据并解析
     * @return
     * @throws Exception
     */
    public static List<Video> getLastVideos() throws Exception{
        String path = "http://192.168.1.100:8080/videoweb/video/list.do";
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setReadTimeout(5*1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        return parseXML(inStream);
    }
    
    /**
     * 从服务端获取json格式的数据并解析
     * @return
     * @throws Exception
     */
    public static List<Video> getJSONLastVideos() throws Exception{
        List<Video> videos = new ArrayList<Video>();
        String path = "http://192.168.1.100:8080/videoweb/video/list.do?format=json";
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setReadTimeout(5*1000);
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        byte[] data = StreamTool.readInputStream(inStream);
        String json = new String(data);
        JSONArray array = new JSONArray(json);
        for(int i=0 ; i < array.length() ; i++){
            JSONObject item = array.getJSONObject(i);
            int id = item.getInt("id");
            String title = item.getString("title");
            int timelength = item.getInt("timelength");
            videos.add(new Video(id, title, timelength));
        }
        return videos;
    }
    /**
     * 解析XML
     * @param inStream
     * @return
     * @throws Exception
     */
    private static List<Video> parseXML(InputStream inStream) throws Exception{
        List<Video> videos = null;
        Video video = null;
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(inStream, "UTF-8");
        int eventType = parser.getEventType();//产生第一个事件
        while(eventType!=XmlPullParser.END_DOCUMENT){//只要不是文档结束事件
            switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                videos = new ArrayList<Video>();
                break;
    
            case XmlPullParser.START_TAG:
                String name = parser.getName();//获取解析器当前指向的元素的名称
                if("video".equals(name)){
                    video = new Video();
                    video.setId(new Integer(parser.getAttributeValue(0)));
                }
                if(video!=null){
                    if("title".equals(name)){
                        video.setTitle(parser.nextText());//获取解析器当前指向元素的下一个文本节点的值
                    }
                    if("timelength".equals(name)){
                        video.setTime(new Integer(parser.nextText()));
                    }
                }
                break;
                
            case XmlPullParser.END_TAG:
                if("video".equals(parser.getName())){
                    videos.add(video);
                    video = null;
                }
                break;
            }
            eventType = parser.next();
        }
        return videos;
    }
}

 

package cn.itcast.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

    /**
     * 从输入流中获取数据
     * @param inStream 输入流
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}

 

posted @ 2016-04-13 15:08  进进  阅读(158)  评论(0)    收藏  举报