android中解析服务器发过来的JSON数据
解析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;
}
}
摘自:com360 博客
相关新闻>>
- 发表评论
-
- 最新评论 更多>>
今日头条
更多>>您可能感兴趣的文章
- Android[中级教程]第一章 数据存储之Shared Preferenc
- Ubuntu 10.10 64bit下使用Android NDK r6编译FFmpeg 0.8.1
- Android开发之Android体系架构介绍
- Android自定义PopupWindow实现的Menu(TabMenu)
- android平板上的GridView视图缓存优化
- Android TextView中设定个别文字字体显示格式
- 自定义ListView背景(解决了拖动变黑的效果)
- Android项目代码混淆
- Android开发者指南(2) —— aapt
- 基于Android NDK的学习之旅-----JNI LOG打印