java实现---中央气象局天气预报接口

一、说明


天气预报的webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

天气预报 Web 服务,数据每2.5小时左右自动更新一次,准确可靠。包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。此天气预报Web Services请不要用于任何商业目的,若有需要请联系我们,欢迎技术交流。 QQ:8409035


二、相关信息调用举例以及说明


1、新建一个Java类:WeatherUtil.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

/**
 * java调用中央气象局天气预报接口
 * 
 * @author Administrator
 * 
 */
public class WeatherUtil {

	/**
	 * 获取一周天气<br>
	 * 方 法 名:getWeekWeatherMap <br>
	 * @param Cityid  城市编码
	 */
	public static List<Map<String, Object>> getWeekWeatherMap(String Cityid)
			throws IOException, NullPointerException {
		// 连接中央气象台的API
		URL url = new URL("http://m.weather.com.cn/data/" + Cityid + ".html");
		URLConnection connectionData = url.openConnection();
		connectionData.setConnectTimeout(1000);
		// 得到1到6天的天气情况
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connectionData.getInputStream(), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = br.readLine()) != null)
				sb.append(line);
			String datas = sb.toString();
			System.out.println(datas);
			JSONObject jsonData = JSONObject.fromObject(datas);
			JSONObject info = jsonData.getJSONObject("weatherinfo");
			for (int i = 1; i <= 6; i++) {
				// 得到未来6天的日期
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DAY_OF_YEAR, i - 1);
				Date date = cal.getTime();
				SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("city", info.getString("city").toString());// 城市
				map.put("date_y", sf.format(date));// 日期
				map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));// 星期
				map.put("fchh", info.getString("fchh").toString());// 发布时间
				map.put("weather", info.getString("weather" + i).toString());// 天气
				map.put("temp", info.getString("temp" + i).toString());// 温度
				map.put("wind", info.getString("wind" + i).toString());// 风况
				map.put("fl", info.getString("fl" + i).toString());// 风速
				// map.put("index", info.getString("index").toString());//
				// 今天的穿衣指数
				// map.put("index_uv", info.getString("index_uv").toString());//
				// 紫外指数
				// map.put("index_tr", info.getString("index_tr").toString());//
				// 旅游指数
				// map.put("index_co", info.getString("index_co").toString());//
				// 舒适指数
				// map.put("index_cl", info.getString("index_cl").toString());//
				// 晨练指数
				// map.put("index_xc", info.getString("index_xc").toString());//
				// 洗车指数
				// map.put("index_d", info.getString("index_d").toString());//
				// 天气详细穿衣指数
				list.add(map);
			}
		} catch (SocketTimeoutException e) {
			System.out.println("连接超时");
		} catch (FileNotFoundException e) {
			System.out.println("加载文件出错");
		}

		return list;

	}

	/**
	 * 
	 * 获取实时天气1<br>
	 * 方 法 名: getTodayWeather <br>
	 * 
	 * @param Cityid
	 *            城市编码
	 */
	public static Map<String, Object> getTodayWeather1(String Cityid)
			throws IOException, NullPointerException {
		// 连接中央气象台的API
		URL url = new URL("http://www.weather.com.cn/data/sk/" + Cityid
				+ ".html");
		URLConnection connectionData = url.openConnection();
		connectionData.setConnectTimeout(1000);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connectionData.getInputStream(), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = br.readLine()) != null)
				sb.append(line);
			String datas = sb.toString();
			System.out.println(datas);
			JSONObject jsonData = JSONObject.fromObject(datas);
			JSONObject info = jsonData.getJSONObject("weatherinfo");
			map.put("city", info.getString("city").toString());// 城市
			map.put("temp", info.getString("temp").toString());// 温度
			map.put("WD", info.getString("WD").toString());// 风向
			map.put("WS", info.getString("WS").toString());// 风速
			map.put("SD", info.getString("SD").toString());// 湿度
			map.put("time", info.getString("time").toString());// 发布时间

		} catch (SocketTimeoutException e) {
			System.out.println("连接超时");
		} catch (FileNotFoundException e) {
			System.out.println("加载文件出错");
		}

		return map;

	}
	
	
	/**
	 * 
	 * 获取实时天气2<br>
	 * 方 法 名: getTodayWeather <br>
	 * 
	 * @param Cityid
	 *            城市编码
	 */
	public static Map<String, Object> getTodayWeather2(String Cityid)
			throws IOException, NullPointerException {
		// 连接中央气象台的API
		URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + Cityid
				+ ".html");
		URLConnection connectionData = url.openConnection();
		connectionData.setConnectTimeout(1000);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					connectionData.getInputStream(), "UTF-8"));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = br.readLine()) != null)
				sb.append(line);
			String datas = sb.toString();
			System.out.println(datas);
			JSONObject jsonData = JSONObject.fromObject(datas);
			JSONObject info = jsonData.getJSONObject("weatherinfo");
			map.put("city", info.getString("city").toString());// 城市
			map.put("temp1", info.getString("temp1").toString());// 最高温度
			map.put("temp2", info.getString("temp2").toString());// 最低温度
			map.put("weather", info.getString("weather").toString());//天气
			map.put("ptime", info.getString("ptime").toString());// 发布时间

		} catch (SocketTimeoutException e) {
			System.out.println("连接超时");
		} catch (FileNotFoundException e) {
			System.out.println("加载文件出错");
		}

		return map;

	}

	private static String getWeek(int iw) {
		String weekStr = "";
		switch (iw) {
		case 1:
			weekStr = "星期天";
			break;
		case 2:
			weekStr = "星期一";
			break;
		case 3:
			weekStr = "星期二";
			break;
		case 4:
			weekStr = "星期三";
			break;
		case 5:
			weekStr = "星期四";
			break;
		case 6:
			weekStr = "星期五";
			break;
		case 7:
			weekStr = "星期六";
			break;
		default:
			break;
		}
		return weekStr;
	}

	public static void main(String[] args) {
		try {
			//测试获取实时天气1(包含风况,湿度)
			Map<String, Object> map = getTodayWeather1("101010100");
			System.out.println(map.get("city") + "\t" + map.get("temp")
					+ "\t" + map.get("WD") + "\t" + map.get("WS")
					+ "\t" + map.get("SD") + "\t" + map.get("time"));
			
			//测试获取实时天气2(包含天气,温度范围)
			Map<String, Object> map2 = getTodayWeather2("101010100");
			System.out.println(map2.get("city") + "\t" + map2.get("temp1")
					+ "\t" + map2.get("temp2") + "\t" + map2.get("weather")
					+ "\t" + map2.get("ptime"));
			
			//测试获取一周天气
			List<Map<String, Object>> listData = getWeekWeatherMap("101010100");
			for (int j = 0; j < listData.size(); j++) {
				Map<String, Object> wMap = listData.get(j);
				System.out.println(wMap.get("city") + "\t" + wMap.get("date_y")
						+ "\t" + wMap.get("week") + "\t" + wMap.get("weather")
						+ "\t" + wMap.get("temp") + "\t" + wMap.get("wind")
						+ "\t" + wMap.get("fl"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值