android中解析服务器发过来的JSON数据

来源:互联网 发布:在线社交网络利弊 编辑:程序博客网 时间:2024/04/30 07:11
解析JSON的效率要比xml高很多,建议在开发中,数据不是很复杂就用JSON传输数据
public class VideoService {public List<Video> getJsonVieos() throws IOException, JSONException{String path = "http://111.14.19.37:8080/vidoe/video/list.do?format=json";URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5 * 1000);conn.setRequestMethod("GET");InputStream is = conn.getInputStream();byte[] data = InputStreamUtil.getByteArray(is);//用自己写的工具类把流转成byte数组String json = new String(data);JSONArray array = new JSONArray(json);List<Video> videos = new ArrayList<Video>();for(int i = 0; i<array.length(); i++){JSONObject jo = array.getJSONObject(i);int id = jo.getInt("id");String title = jo.getString("title");int timelength = jo.getInt("timelength");videos.add(new Video(id, title, timelength));}return videos;}}

原创粉丝点击