首页 空气质量指数 空气质量指数示例代码 历史空气质量指数查询[Java]

历史空气质量指数查询示例代码[Java]

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

历史空气质量指数查询

package api.jisuapi.aqi;

import java.net.URLEncoder;

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

public class History {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/aqi/history";
	public static final String city = "杭州";// utf-8

	public static void Get() throws Exception {
		String result = null;
		String url = URL + "?appkey=" + APPKEY + "&city=" + URLEncoder.encode(city,"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 cityid = resultarr.getString("cityid");
				String city = resultarr.getString("city");
				System.out.println(cityid + " " + city);
				if (resultarr.opt("hour") != null) {
					JSONArray hour = resultarr.optJSONArray("hour");
					for (int i = 0; i < hour.size(); i++) {
						JSONObject obj = (JSONObject) hour.opt(i);
						String so2 = obj.getString("so2");
						String so224 = obj.getString("so224");
						String no2 = obj.getString("no2");
						String no224 = obj.getString("no224");
						String co = obj.getString("co");
						String co24 = obj.getString("co24");
						String o3 = obj.getString("o3");
						String o38 = obj.getString("o38");
						String o324 = obj.getString("o324");
						String pm10 = obj.getString("pm10");
						String pm1024 = obj.getString("pm1024");
						String pm2_5 = obj.getString("pm2_5");
						String pm2_524 = obj.getString("pm2_524");
						String iso2 = obj.getString("iso2");
						String ino2 = obj.getString("ino2");
						String ico = obj.getString("ico");
						String io3 = obj.getString("io3");
						String io38 = obj.getString("io38");
						String ipm10 = obj.getString("ipm10");
						String ipm2_5 = obj.getString("ipm2_5");
						String aqi = obj.getString("aqi");
						String primarypollutant = obj.getString("primarypollutant");
						String quality = obj.getString("quality");
						String timepoint = obj.getString("timepoint");
						String color = obj.getString("color");
						System.out.println(so2 + " " + so224 + " " + no2 + " " + no224 + " " + co + " " + co24 + " "
								+ o3 + " " + o38 + " " + o324 + " " + pm10 + " " + pm1024 + " " + pm2_5 + " " + pm2_524
								+ " " + iso2 + " " + ino2 + " " + ico + " " + io3 + " " + io38 + " " + ipm10 + " "
								+ ipm2_5 + " " + aqi + " " + primarypollutant + " " + quality + " " + timepoint + " "
								+ color);
					}
				}
				if (resultarr.opt("day") != null) {
					JSONArray day = resultarr.optJSONArray("day");
					for (int i = 0; i < day.size(); i++) {
						JSONObject obj = (JSONObject) day.opt(i);
						String cityid_ = obj.getString("cityid");
						String so2 = obj.getString("so2");
						String so224 = obj.getString("so224");
						String no2 = obj.getString("no2");
						String no224 = obj.getString("no224");
						String co = obj.getString("co");
						String co24 = obj.getString("co24");
						String o3 = obj.getString("o3");
						String o38 = obj.getString("o38");
						String o324 = obj.getString("o324");
						String pm10 = obj.getString("pm10");
						String pm1024 = obj.getString("pm1024");
						String pm2_5 = obj.getString("pm2_5");
						String pm2_524 = obj.getString("pm2_524");
						String iso2 = obj.getString("iso2");
						String ino2 = obj.getString("ino2");
						String ico = obj.getString("ico");
						String io3 = obj.getString("io3");
						String io38 = obj.getString("io38");
						String ipm10 = obj.getString("ipm10");
						String ipm2_5 = obj.getString("ipm2_5");
						String aqi = obj.getString("aqi");
						String primarypollutant = obj.getString("primarypollutant");
						String quality = obj.getString("quality");
						String date = obj.getString("date");
						String color = obj.getString("color");
						System.out.println(cityid + " " + so2 + " " + so224 + " " + no2 + " " + no224 + " " + co + " "
								+ co24 + " " + o3 + " " + o38 + " " + o324 + " " + pm10 + " " + pm1024 + " " + pm2_5
								+ " " + pm2_524 + " " + iso2 + " " + ino2 + " " + ico + " " + io3 + " " + io38 + " "
								+ ipm10 + " " + ipm2_5 + " " + aqi + " " + primarypollutant + " " + quality + " " + date
								+ " " + color);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}