首页 新华字典 新华字典示例代码 新华字典查询[Java]

新华字典查询示例代码[Java]

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

新华字典查询

package api.jisuapi.zidian;

import java.net.URLEncoder;

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

public class Word {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/zidian/word";
	public static final String word = "好";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&word=" + URLEncoder.encode(word, "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 pinyin = resultarr.getString("pinyin");
				String bihua = resultarr.getString("bihua");
				String bushou = resultarr.getString("bushou");
				String jiegou = resultarr.getString("jiegou");
				String bishun = resultarr.getString("bishun");
				String wubi = resultarr.getString("wubi");
				System.out.println(
						name + " " + pinyin + " " + bihua + " " + bushou + " " + jiegou + " " + bishun + " " + wubi);
				JSONArray english = resultarr.optJSONArray("english");
				for (int i = 0; i < english.size(); i++) {
					System.out.print(english.get(i) + " ");
				}
				JSONArray explain = resultarr.optJSONArray("explain");
				for (int i = 0; i < explain.size(); i++) {
					JSONObject obj = explain.optJSONObject(i);
					String pinyin1 = obj.getString("pinyin");
					String content = obj.getString("content");
					System.out.println(pinyin1 + " " + content);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}