JuheWeather

package weather;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

public class JuheWeather {
	public static void main(String[] args) {
		showWeather();
	}

	@SuppressWarnings("unchecked")
	public static void showWeather() {
		StringBuffer sb = new StringBuffer();
		String charset = "UTF-8";
		String urlnameWeather = "http://v.juhe.cn/weather/index?"
				+ "cityname=%E4%B8%8A%E6%B5%B7&dtype=json&format=2&key=d2959247af73fb35f211f39c4b141165";
		String weather = get(urlnameWeather, charset);// 得到JSON字符串
		String urlnameCityAir = "http://web.juhe.cn:8080/environment/air/cityair?"
				+ "city=%E4%B8%8A%E6%B5%B7&key=23f6d70f4f29917eb0aefe15a344e257";
		Map<String, Object> weatherMap = (Map<String, Object>) JSON.parseObject(weather, Map.class).get("result");
		String cityair = get(urlnameCityAir, charset);// 得到JSON字符串;
		Map<String, Object> cityairMap = ((List<Map<String, Object>>) JSON.parseObject(cityair, Map.class)
				.get("result")).get(0);
		Map<String, Object> citynow = (Map<String, Object>) cityairMap.get("citynow");
		String AQI = (String) citynow.get("AQI");
		String quality = (String) citynow.get("quality");
		List<Map<String, Object>> future = (List<Map<String, Object>>) weatherMap.get("future");
		Map<String, Object> sk = (Map<String, Object>) weatherMap.get("sk");
		Map<String, Object> today = (Map<String, Object>) weatherMap.get("today");
		sb.append(
				"温度:" + sk.get("temp") + "℃" + " 天气:" + today.get("weather") + " 空气:" + AQI + "\r\n");
		for (Map<String, Object> m2 : future) {
			sb.append(m2.get("week").toString().replace("星期","周") + " " + m2.get("temperature") + " "
					+ m2.get("weather") + "\n");
		}
		System.out.println(sb);
	}

	public static String fmt(String str) {
		return str.substring(0, 4) + "-" + str.substring(4, 6) + "-" + str.substring(6, 8);
	}

	/**
	 * 从URL取数据
	 */
	public static String get(String urlAll, String charset) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
		try {
			URL url = new URL(urlAll);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.setReadTimeout(30000);
			connection.setConnectTimeout(30000);
			connection.setRequestProperty("User-agent", userAgent);
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, charset));
			String strRead = null;
			while ((strRead = reader.readLine()) != null) {
				sbf.append(strRead);
				sbf.append("\r\n");
			}
			reader.close();
			result = sbf.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

赠人玫瑰 手留余香
CONKY 说明
Axis1.4 client
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论