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

单位换算示例代码[Java]

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

单位换算

package api.jisuapi.unitconvert;

import java.net.URLEncoder;

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

public class Convert {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/unitconvert/convert";
	public static final String from = "千米";// utf-8
	public static final String to = "米";// utf-8
	public static final String amount = "5";

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

		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 from = resultarr.getString("from");
				String to = resultarr.getString("to");
				String fromename = resultarr.getString("fromename");
				String toename = resultarr.getString("toename");
				String fromsymbol = resultarr.getString("fromsymbol");
				String tosymbol = resultarr.getString("tosymbol");
				String rate = resultarr.getString("rate");
				String camount = resultarr.getString("camount");
				System.out.println(from + " " + to + " " + fromename + " " + toename + " " + fromsymbol + " " + tosymbol
						+ " " + rate + " " + camount);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}