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

一、说明

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

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


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


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

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.net.SocketTimeoutException;  
  6. import java.net.URL;  
  7. import java.net.URLConnection;  
  8. import java.text.SimpleDateFormat;  
  9. import java.util.ArrayList;  
  10. import java.util.Calendar;  
  11. import java.util.Date;  
  12. import java.util.HashMap;  
  13. import java.util.List;  
  14. import java.util.Map;  
  15.   
  16. import net.sf.json.JSONObject;  
  17.   
  18. /** 
  19.  * java调用中央气象局天气预报接口 
  20.  *  
  21.  * @author Administrator 
  22.  *  
  23.  */  
  24. public class WeatherUtil {  
  25.   
  26.     /** 
  27.      * 获取一周天气<br> 
  28.      * 方 法 名:getWeekWeatherMap <br> 
  29.      * @param Cityid  城市编码 
  30.      */  
  31.     public static List<Map<String, Object>> getWeekWeatherMap(String Cityid)  
  32.             throws IOException, NullPointerException {  
  33.         // 连接中央气象台的API  
  34.         URL url = new URL("http://m.weather.com.cn/data/" + Cityid + ".html");  
  35.         URLConnection connectionData = url.openConnection();  
  36.         connectionData.setConnectTimeout(1000);  
  37.         // 得到1到6天的天气情况  
  38.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  39.         try {  
  40.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  41.                     connectionData.getInputStream(), "UTF-8"));  
  42.             StringBuilder sb = new StringBuilder();  
  43.             String line = null;  
  44.             while ((line = br.readLine()) != null)  
  45.                 sb.append(line);  
  46.             String datas = sb.toString();  
  47.             System.out.println(datas);  
  48.             JSONObject jsonData = JSONObject.fromObject(datas);  
  49.             JSONObject info = jsonData.getJSONObject("weatherinfo");  
  50.             for (int i = 1; i <= 6; i++) {  
  51.                 // 得到未来6天的日期  
  52.                 Calendar cal = Calendar.getInstance();  
  53.                 cal.add(Calendar.DAY_OF_YEAR, i - 1);  
  54.                 Date date = cal.getTime();  
  55.                 SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");  
  56.                 Map<String, Object> map = new HashMap<String, Object>();  
  57.                 map.put("city", info.getString("city").toString());// 城市  
  58.                 map.put("date_y", sf.format(date));// 日期  
  59.                 map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));// 星期  
  60.                 map.put("fchh", info.getString("fchh").toString());// 发布时间  
  61.                 map.put("weather", info.getString("weather" + i).toString());// 天气  
  62.                 map.put("temp", info.getString("temp" + i).toString());// 温度  
  63.                 map.put("wind", info.getString("wind" + i).toString());// 风况  
  64.                 map.put("fl", info.getString("fl" + i).toString());// 风速  
  65.                 // map.put("index", info.getString("index").toString());//  
  66.                 // 今天的穿衣指数  
  67.                 // map.put("index_uv", info.getString("index_uv").toString());//  
  68.                 // 紫外指数  
  69.                 // map.put("index_tr", info.getString("index_tr").toString());//  
  70.                 // 旅游指数  
  71.                 // map.put("index_co", info.getString("index_co").toString());//  
  72.                 // 舒适指数  
  73.                 // map.put("index_cl", info.getString("index_cl").toString());//  
  74.                 // 晨练指数  
  75.                 // map.put("index_xc", info.getString("index_xc").toString());//  
  76.                 // 洗车指数  
  77.                 // map.put("index_d", info.getString("index_d").toString());//  
  78.                 // 天气详细穿衣指数  
  79.                 list.add(map);  
  80.             }  
  81.         } catch (SocketTimeoutException e) {  
  82.             System.out.println("连接超时");  
  83.         } catch (FileNotFoundException e) {  
  84.             System.out.println("加载文件出错");  
  85.         }  
  86.   
  87.         return list;  
  88.   
  89.     }  
  90.   
  91.     /** 
  92.      *  
  93.      * 获取实时天气1<br> 
  94.      * 方 法 名: getTodayWeather <br> 
  95.      *  
  96.      * @param Cityid 
  97.      *            城市编码 
  98.      */  
  99.     public static Map<String, Object> getTodayWeather1(String Cityid)  
  100.             throws IOException, NullPointerException {  
  101.         // 连接中央气象台的API  
  102.         URL url = new URL("http://www.weather.com.cn/data/sk/" + Cityid  
  103.                 + ".html");  
  104.         URLConnection connectionData = url.openConnection();  
  105.         connectionData.setConnectTimeout(1000);  
  106.         Map<String, Object> map = new HashMap<String, Object>();  
  107.         try {  
  108.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  109.                     connectionData.getInputStream(), "UTF-8"));  
  110.             StringBuilder sb = new StringBuilder();  
  111.             String line = null;  
  112.             while ((line = br.readLine()) != null)  
  113.                 sb.append(line);  
  114.             String datas = sb.toString();  
  115.             System.out.println(datas);  
  116.             JSONObject jsonData = JSONObject.fromObject(datas);  
  117.             JSONObject info = jsonData.getJSONObject("weatherinfo");  
  118.             map.put("city", info.getString("city").toString());// 城市  
  119.             map.put("temp", info.getString("temp").toString());// 温度  
  120.             map.put("WD", info.getString("WD").toString());// 风向  
  121.             map.put("WS", info.getString("WS").toString());// 风速  
  122.             map.put("SD", info.getString("SD").toString());// 湿度  
  123.             map.put("time", info.getString("time").toString());// 发布时间  
  124.   
  125.         } catch (SocketTimeoutException e) {  
  126.             System.out.println("连接超时");  
  127.         } catch (FileNotFoundException e) {  
  128.             System.out.println("加载文件出错");  
  129.         }  
  130.   
  131.         return map;  
  132.   
  133.     }  
  134.       
  135.       
  136.     /** 
  137.      *  
  138.      * 获取实时天气2<br> 
  139.      * 方 法 名: getTodayWeather <br> 
  140.      *  
  141.      * @param Cityid 
  142.      *            城市编码 
  143.      */  
  144.     public static Map<String, Object> getTodayWeather2(String Cityid)  
  145.             throws IOException, NullPointerException {  
  146.         // 连接中央气象台的API  
  147.         URL url = new URL("http://www.weather.com.cn/data/cityinfo/" + Cityid  
  148.                 + ".html");  
  149.         URLConnection connectionData = url.openConnection();  
  150.         connectionData.setConnectTimeout(1000);  
  151.         Map<String, Object> map = new HashMap<String, Object>();  
  152.         try {  
  153.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  154.                     connectionData.getInputStream(), "UTF-8"));  
  155.             StringBuilder sb = new StringBuilder();  
  156.             String line = null;  
  157.             while ((line = br.readLine()) != null)  
  158.                 sb.append(line);  
  159.             String datas = sb.toString();  
  160.             System.out.println(datas);  
  161.             JSONObject jsonData = JSONObject.fromObject(datas);  
  162.             JSONObject info = jsonData.getJSONObject("weatherinfo");  
  163.             map.put("city", info.getString("city").toString());// 城市  
  164.             map.put("temp1", info.getString("temp1").toString());// 最高温度  
  165.             map.put("temp2", info.getString("temp2").toString());// 最低温度  
  166.             map.put("weather", info.getString("weather").toString());//天气  
  167.             map.put("ptime", info.getString("ptime").toString());// 发布时间  
  168.   
  169.         } catch (SocketTimeoutException e) {  
  170.             System.out.println("连接超时");  
  171.         } catch (FileNotFoundException e) {  
  172.             System.out.println("加载文件出错");  
  173.         }  
  174.   
  175.         return map;  
  176.   
  177.     }  
  178.   
  179.     private static String getWeek(int iw) {  
  180.         String weekStr = "";  
  181.         switch (iw) {  
  182.         case 1:  
  183.             weekStr = "星期天";  
  184.             break;  
  185.         case 2:  
  186.             weekStr = "星期一";  
  187.             break;  
  188.         case 3:  
  189.             weekStr = "星期二";  
  190.             break;  
  191.         case 4:  
  192.             weekStr = "星期三";  
  193.             break;  
  194.         case 5:  
  195.             weekStr = "星期四";  
  196.             break;  
  197.         case 6:  
  198.             weekStr = "星期五";  
  199.             break;  
  200.         case 7:  
  201.             weekStr = "星期六";  
  202.             break;  
  203.         default:  
  204.             break;  
  205.         }  
  206.         return weekStr;  
  207.     }  
  208.   
  209.     public static void main(String[] args) {  
  210.         try {  
  211.             //测试获取实时天气1(包含风况,湿度)  
  212.             Map<String, Object> map = getTodayWeather1("101010100");  
  213.             System.out.println(map.get("city") + "\t" + map.get("temp")  
  214.                     + "\t" + map.get("WD") + "\t" + map.get("WS")  
  215.                     + "\t" + map.get("SD") + "\t" + map.get("time"));  
  216.               
  217.             //测试获取实时天气2(包含天气,温度范围)  
  218.             Map<String, Object> map2 = getTodayWeather2("101010100");  
  219.             System.out.println(map2.get("city") + "\t" + map2.get("temp1")  
  220.                     + "\t" + map2.get("temp2") + "\t" + map2.get("weather")  
  221.                     + "\t" + map2.get("ptime"));  
  222.               
  223.             //测试获取一周天气  
  224.             List<Map<String, Object>> listData = getWeekWeatherMap("101010100");  
  225.             for (int j = 0; j < listData.size(); j++) {  
  226.                 Map<String, Object> wMap = listData.get(j);  
  227.                 System.out.println(wMap.get("city") + "\t" + wMap.get("date_y")  
  228.                         + "\t" + wMap.get("week") + "\t" + wMap.get("weather")  
  229.                         + "\t" + wMap.get("temp") + "\t" + wMap.get("wind")  
  230.                         + "\t" + wMap.get("fl"));  
  231.             }  
  232.               
  233.         } catch (Exception e) {  
  234.             e.printStackTrace();  
  235.         }  
  236.     }  
  237. }  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值