首页 成语大全 成语大全示例代码 成语查询[Java]

成语查询示例代码[Java]

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

成语查询

package api.jisuapi.chengyu;

import java.net.URLEncoder;

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

public class Detail {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/chengyu/detail";
	public static final String chengyu = "叶公好龙";// utf-8

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

		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 name = resultarr.getString("name");
				String pronounce = resultarr.getString("pronounce");
				String content = resultarr.getString("content");
				String comefrom = resultarr.getString("comefrom");
				System.out.println(name + " " + pronounce + " " + content + " " + comefrom);
				if (resultarr.opt("antonym") != null) {
					JSONArray antonym = resultarr.optJSONArray("antonym");
					for (int i = 0; i < antonym.size(); i++) {
						System.out.print(antonym.get(i) + " ");
					}
				}
				if (resultarr.opt("thesaurus") != null) {
					JSONArray thesaurus = resultarr.optJSONArray("thesaurus");
					for (int i = 0; i < thesaurus.size(); i++) {
						System.out.print(thesaurus.get(i) + " ");
					}
				}
				System.out.println();
				String example = resultarr.getString("example");
				System.out.println(example);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}