首页 绕口令 绕口令示例代码 绕口令查询[Java]

绕口令查询示例代码[Java]

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

绕口令查询

package api.jisuapi.rkl;

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/rkl/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 title = obj.getString("title");
						System.out.println(title + " " + content);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}