首页 歇后语 歇后语示例代码 歇后语查询[Java]

歇后语查询示例代码[Java]

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

歇后语查询

package api.jisuapi.xhy;

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/xhy/search";
	public static final int pagenum = 1;
	public static final int pagesize = 1;
	public static final String keyword = "女人";// utf-8

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

		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 = json.optJSONObject("result");
				String total = resultarr.getString("total");
				String pagenum = resultarr.getString("pagenum");
				String pagesize = resultarr.getString("pagesize");
				System.out.println(total + " " + pagenum + " " + pagesize);
				if (resultarr.opt("list") != null) {
					JSONArray list = resultarr.optJSONArray("list");
					for (int i = 0; i < list.size(); i++) {
						JSONObject obj = (JSONObject) list.opt(i);
						String content = obj.getString("content");
						String answer = obj.getString("answer");
						System.out.println(content + " " + answer);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}