首页 今日油价 今日油价示例代码 省市油价查询[Java]

省市油价查询示例代码[Java]

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

省市油价查询

package api.jisuapi.oil;

import java.net.URLEncoder;

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

public class Query {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/oil/query";
	public static final String province = "河南";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&province=" + URLEncoder.encode(province, "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 province = resultarr.getString("province");
				String oil90 = resultarr.getString("oil90");
				String oil93 = resultarr.getString("oil93");
				String oil97 = resultarr.getString("oil97");
				String oil0 = resultarr.getString("oil0");
				String updatetime = resultarr.getString("updatetime");
				System.out.println(province + " " + oil90 + " " + oil93 + " " + oil97 + " " + oil0 + " " + updatetime);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}