import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class XinLang {
static String url = "http://zhibo.sina.com.cn/api/zhibo/feed?\\%20callback=jQuery0&page=1&page_size=1&zhibo_id=152\\%20&tag_id=0&dire=f&dpc=1&pagesize=1&_=0%20Request%20Method:GET";
public static void main(String[] args) {
List<DataInfo> infoList = null;
while (true){
try {
Thread.sleep(1000);
List<DataInfo> dataInfos = resultData(url);
if(infoList != null && infoList.get(0).getText().equals(dataInfos.get(0).getText())){
continue;
}
infoList = dataInfos ;
System.out.println("=========================================================");
for(DataInfo dataInfo :dataInfos){
System.out.println(dataInfo.getText());
System.out.println(dataInfo.getCrateTime());
System.out.println(dataInfo.getUrl());
}
System.out.println("=========================================================");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static List<DataInfo> resultData(String url){
String data = doGet(url);
if(data == null || "".equals(data)){
System.out.println("没有结果");
return null ;
}
JSONObject jsonObject = JSON.parseObject(data) ;
JSONObject resultJson = jsonObject.getJSONObject("result");
JSONObject statusJson = resultJson.getJSONObject("status");
if(!"OK".equals(statusJson.getString("msg")) && !"0".equals(statusJson.getString("code"))){
System.out.println(resultJson);
System.out.println("查询失败");
return null;
}
JSONObject dataJson = resultJson.getJSONObject("data");
if( "".equals(dataJson)){
System.out.println("data" + dataJson);
System.out.println("无结果");
return null;
}
// System.out.println(dataJson.getString("feed"));
JSONArray array = dataJson.getJSONObject("feed").getJSONArray("list");
List<DataInfo> list = new ArrayList<>();
for(int i = 0 ; i< array.size() ; i++){
JSONObject listJson = array.getJSONObject(i);
DataInfo dataInfo = new DataInfo();
dataInfo.setText(listJson.getString("rich_text"));
dataInfo.setCrateTime(listJson.getString("create_time"));
dataInfo.setUrl(listJson.getString("docurl"));
list.add(dataInfo);
}
return list;
}
public static String doGet(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(httpurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
static class DataInfo {
private String text;
private String crateTime;
private String url;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getCrateTime() {
return crateTime;
}
public void setCrateTime(String crateTime) {
this.crateTime = crateTime;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}