现在的位置: 首页 > 综合 > 正文

请求Url的json数据

2017年12月16日 ⁄ 综合 ⁄ 共 876字 ⁄ 字号 评论关闭

public List<News> getJSONLastNews() throws Exception{

String path="http://192.168.1.100:8080/videonews/ListService?format=json";
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inStream=conn.getInputStream();
return parseJSON(inStream);
}
return null;
}
public List<News> parseJSON(inStream) throws Exception{

List<News> newses=new ArrayList<News>();
byte[] data = StreamTool.read(inStream);
String json= new String[data];
JSONArray array=new JSONArray(json);
for(int i=0;i < array.length ; i++){
JSONObject jsonObject = array.getJSONObject(i);
News news=new News(jsonObject.getInt("id"),jsonObject.getString("title"),jsonObject.getInt(timeLenth));
newses.add(news);
}
return newses;

}

News.java

public class News{

private Integer id;

private String title;

private Integer timeLenth;

//...getter,setter方法

}

抱歉!评论已关闭.