首页 单位换算 单位换算示例代码 单个单位[Java]

单个单位示例代码[Java]

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

单个单位

package api.jisuapi.unitconvert;

import java.net.URLEncoder;

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

public class Single {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/unitconvert/single";
	public static final String unit = "千米";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&unit=" + URLEncoder.encode(unit, "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 unit = resultarr.getString("unit");
				String ename = resultarr.getString("ename");
				String symbol = resultarr.getString("symbol");
				String amount = resultarr.getString("amount");
				System.out.println(unit + " " + ename + " " + symbol + " " + amount);
				if (resultarr.opt("list") != null) {
					JSONObject list = resultarr.optJSONObject("list");
					if (list.opt("光年") != null) {
						JSONObject guang = list.optJSONObject("光年");
						String ename1 = guang.getString("ename");
						String rate = guang.getString("rate");
						String camount = guang.getString("camount");
						System.out.println(ename1 + " " + rate + " " + camount);
					}
					if (list.opt("天文单位") != null) {
						JSONObject tian = list.optJSONObject("天文单位");
						String ename1 = tian.getString("ename");
						String rate = tian.getString("rate");
						String camount = tian.getString("camount");
						System.out.println(ename1 + " " + rate + " " + camount);
					}
					if (list.opt("米") != null) {
						JSONObject mi = list.optJSONObject("米");
						String ename1 = mi.getString("ename");
						String rate = mi.getString("rate");
						String camount = mi.getString("camount");
						System.out.println(ename1 + " " + rate + " " + camount);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}