首页 新闻 新闻示例代码 搜索新闻[Java]

搜索新闻示例代码[Java]

作者:xiezhongpian 阅读数:1798 上传时间:2017-05-09

搜索新闻

package api.jisuapi.news;

import java.net.URLEncoder;

import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Search {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/news/search";
	public static final String keyword = "姚明";// utf8 关键字

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?keyword=" + URLEncoder.encode(keyword, "utf-8") + "&appkey=" + APPKEY;

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONObject resultarr = (JSONObject) json.opt("result");
				String keyword = resultarr.getString("keyword");
				String num = resultarr.getString("num");
				System.out.println(keyword + " " + num);
				JSONArray list = resultarr.optJSONArray("list");
				for (int i = 0; i < list.size(); i++) {
					JSONObject obj = (JSONObject) list.opt(i);
					String title = obj.getString("title");
					String time = obj.getString("time");
					String src = obj.getString("src");
					String category = obj.getString("category");
					String pic = obj.getString("pic");
					String content = obj.getString("content");
					String url1 = obj.getString("url");
					String weburl = obj.getString("weburl");
					System.out.println(title + " " + time + " " + src + " " + category + " " + pic + " " + url1 + " "
							+ weburl + " " + content);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}