首页 全国天气预报 全国天气预报示例代码 天气预报查询[Java]

天气预报查询示例代码[Java]

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

天气预报查询

package api.binstd.weather;

import java.net.URLEncoder;

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

public class Query {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "http://api.binstd.com/weather/query";
	public static final String city = "安顺";// utf-8
	public static final int cityid = 111;// 任选
	public static final String citycode = "101260301";// 任选

	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 city = resultarr.getString("city");
				String cityid = resultarr.getString("cityid");
				String citycode = resultarr.getString("citycode");
				String date = resultarr.getString("date");
				String week = resultarr.getString("week");
				String weather = resultarr.getString("weather");
				String temp = resultarr.getString("temp");
				String temphigh = resultarr.getString("temphigh");
				String templow = resultarr.getString("templow");
				String img = resultarr.getString("img");
				String humidity = resultarr.getString("humidity");
				String pressure = resultarr.getString("pressure");
				String windspeed = resultarr.getString("windspeed");
				String winddirect = resultarr.getString("winddirect");
				System.out.println(city + " " + cityid + " " + citycode + " " + date + " " + week + " " + weather + " "
						+ temp + " " + temphigh + " " + templow + " " + img + " " + humidity + " " + pressure + " "
						+ windspeed + " " + winddirect);
				if (resultarr.opt("index") != null) {
					JSONArray index = resultarr.optJSONArray("index");
					for (int i = 0; i < index.size(); i++) {
						JSONObject obj = (JSONObject) index.opt(i);
						String iname = obj.getString("iname");
						String ivalue = obj.getString("ivalue");
						String detail = obj.getString("detail");
						System.out.println("指数:" + iname + " " + ivalue + " " + detail);
					}
				}
				if (resultarr.opt("aqi") != null) {
					JSONObject aqi = resultarr.optJSONObject("aqi");
					String so2 = aqi.getString("so2");
					String so224 = aqi.getString("so224");
					String no2 = aqi.getString("no2");
					String no224 = aqi.getString("no224");
					String co = aqi.getString("co");
					String co24 = aqi.getString("co24");
					String o3 = aqi.getString("o3");
					String o38 = aqi.getString("o38");
					String o324 = aqi.getString("o324");
					String pm10 = aqi.getString("pm10");
					String pm1024 = aqi.getString("pm1024");
					String pm2_5 = aqi.getString("pm2_5");
					String pm2_524 = aqi.getString("pm2_524");
					String iso2 = aqi.getString("iso2");
					String ino2 = aqi.getString("ino2");
					String ico = aqi.getString("ico");
					String io3 = aqi.getString("io3");
					String io38 = aqi.getString("io38");
					String ipm10 = aqi.getString("ipm10");
					String ipm2_5 = aqi.getString("ipm2_5");
					String aqi1 = aqi.getString("aqi");
					String primarypollutant = aqi.getString("primarypollutant");
					String quality = aqi.getString("quality");
					String timepoint = aqi.getString("timepoint");
					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 + " " + aqi1 + " " + primarypollutant + " " + quality + " " + timepoint);
					if (aqi.opt("aqiinfo") != null) {
						JSONObject aqiinfo = aqi.optJSONObject("aqiinfo");
						String level = aqiinfo.getString("level");
						String color = aqiinfo.getString("color");
						String affect = aqiinfo.getString("affect");
						String measure = aqiinfo.getString("measure");
						System.out.println(level + " " + color + " " + affect + " " + measure);
					}
				}
				if (resultarr.opt("daily") != null) {
					JSONArray daily = resultarr.optJSONArray("daily");
					for (int i = 0; i < daily.size(); i++) {
						JSONObject obj = (JSONObject) daily.opt(i);
						String date1 = obj.getString("date");
						String week1 = obj.getString("week");
						String sunrise = obj.getString("sunrise");
						String sunset = obj.getString("sunset");
						System.out.println("未来几天天气:" + date1 + " " + week1 + " " + sunrise + " " + sunset);
						if (obj.opt("night") != null) {
							JSONObject night = (JSONObject) obj.opt("night");
							String weather1 = night.getString("weather");
							String templow1 = night.getString("templow");
							String img1 = night.getString("img");
							String winddirect1 = night.getString("winddirect");
							String windpower1 = night.getString("windpower");
							System.out.println(
									weather1 + " " + templow1 + " " + img1 + " " + winddirect1 + " " + windpower1);
						}
						if (obj.opt("day") != null) {
							JSONObject day = obj.optJSONObject("day");
							String weather1 = day.getString("weather");
							String templow1 = "";
							if (day.has("templow")) {
								templow1 = day.getString("templow");
							}
							String img1 = day.getString("img");
							String winddirect1 = day.getString("winddirect");
							String windpower1 = day.getString("windpower");
							System.out.println(
									weather1 + " " + templow1 + " " + img1 + " " + winddirect1 + " " + windpower1);
						}
					}
				}
				if (resultarr.opt("hourly") != null) {
					JSONArray hourly = resultarr.optJSONArray("hourly");
					for (int i = 0; i < hourly.size(); i++) {
						JSONObject obj = (JSONObject) hourly.opt(i);
						String time = obj.getString("time");
						String weather1 = obj.getString("weather");
						String temp1 = obj.getString("temp");
						String img1 = obj.getString("img");
						System.out.println("未来几小时天气:" + time + " " + weather1 + " " + temp1 + " " + img1);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}