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 博客
相关新闻>>
- 发表评论
-
- 最新评论 更多>>