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方法
}