首页 >> 大全

利用中央气象台的API获取天气

2023-06-30 大全 44 作者:考证青年

小项目算是用三层结构来构架,最上面一层就不写了,直接在层测试。

中央气象台的接口标准文档我没找到,所以也是参照了别人的应用自己写了一个。。

目录结构为:

中央气象未来十天气象__气象接口api

这个是根节点

返回的表的结构式一定的,所以我直接配置到了本地

如下:

气象接口api__中央气象未来十天气象

.xml







































第二层是访问省级节点:例如:

返回:

第三层是访问市级节点:例如:

返回值:

气象接口api_中央气象未来十天气象_

{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年9月3日","date":"","week":"星期二","fchh":"08","cityid":"101010100","temp1":"27℃~18℃","temp2":"22℃~17℃","temp3":"24℃~17℃","temp4":"29℃~17℃","temp5":"29℃~18℃","temp6":"28℃~18℃","tempF1":"80.6℉~64.4℉","tempF2":"71.6℉~62.6℉","tempF3":"75.2℉~62.6℉","tempF4":"84.2℉~62.6℉","tempF5":"84.2℉~64.4℉","tempF6":"82.4℉~64.4℉","weather1":"多云转阵雨","weather2":"中雨转小雨","weather3":"小雨转晴","weather4":"晴","weather5":"晴","weather6":"晴转多云","img1":"1","img2":"3","img3":"8","img4":"7","img5":"7","img6":"0","img7":"0","img8":"99","img9":"0","img10":"99","img11":"0","img12":"1","img_single":"1","img_title1":"多云","img_title2":"阵雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"晴","img_title7":"晴","img_title8":"晴","img_title9":"晴","img_title10":"晴","img_title11":"晴","img_title12":"多云","img_title_single":"多云","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"热","index_d":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","index48":"较舒适","index48_d":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"27","st2":"16","st3":"18","st4":"15","st5":"22","st6":"18","index_cl":"适宜","index_ls":"适宜","index_ag":"易发"}}

层:

package com.lxl.getweather;import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class GetWeatherService {GetWeatherDao dao;public GetWeatherService() {dao=new GetWeatherDao();}/*** 获取城市气象信息的流程* @param province* @param selectcity* @return*/public CityWeather getWeatherDay(String province,String selectcity){//首先,获取所有省份名和拼音的mapMap provincemap = getProvinceMap();//根据所有的省份名 获取 所需要的省份名拼音String provincexml = provincemap.get(province);if(provincexml==null){//有问题,省份不存在return null;}//其次,输入值省份名拼音,获取指定市的城市气象idMap citymap = getCityMap(provincexml);if(citymap==null){//有问题,城市不存在return null;}String cityid = citymap.get(selectcity);//根据id获取城市的json数据,并且解析该数据返回城市未来七天气象信息CityWeather cityWeather = getCityWeather(cityid);if(cityWeather==null){//有问题,解析不正确return null;}return cityWeather;}/*** 获取所有省份名和拼音的map*/public Map getProvinceMap() {Map proMap = new HashMap();try {SAXReader sr = new SAXReader();// 获取读取xml的对象。Document document = sr.read(new File("src\\province.xml"));Element root = document.getRootElement();List elementlist = root.elements("city");for (Object obj : elementlist) {Element row = (Element) obj;String quName = row.attribute("quName").getText();String pyName = row.attribute("pyName").getText();proMap.put(quName, pyName);}} catch (Exception e) {e.printStackTrace();return null;}return proMap;}/*** 获取指定省份的城市列表*/public Map getCityMap(String provincexml) {InputStream cityXml = dao.getCityXml(provincexml);Map citymap=new HashMap<>();try {SAXReader sr = new SAXReader();// 获取读取xml的对象。Document document = sr.read(cityXml);Element root = document.getRootElement();List elementlist = root.elements("city");for (Object obj : elementlist) {Element row = (Element) obj;String cityname = row.attribute("cityname").getText();String cityid =row.attribute("url").getText();citymap.put(cityname,cityid);}} catch (Exception e) {e.printStackTrace();return null;}return citymap;}/*** 根据指定的城市气象id获取该城市未来七天的天气信息*/public CityWeather getCityWeather(String cityid) {CityWeather cityWeather=null;String weatherJson = dao.getWeatherJson(cityid);weatherJson="["+weatherJson+"]";System.out.println(weatherJson);//将json字符串转成json对象JSONArray jsons = JSONArray.fromObject(weatherJson);int jsonLength = jsons.size();//对json数组进行循环,一般应该只返回一个。for (int i = 0; i < jsonLength; i++) {JSONObject tempJson = JSONObject.fromObject(jsons.get(i));Object object = tempJson.get("weatherinfo");JSONObject weatherday = JSONObject.fromObject(object);cityWeather=new CityWeather();String city = weatherday.get("city").toString();String city_en = weatherday.get("city_en").toString();String date_y = weatherday.get("date_y").toString();String week = weatherday.get("week").toString();String temp1 = weatherday.get("temp1").toString();//今天温度String temp2 = weatherday.get("temp2").toString();//明天温度String temp3 = weatherday.get("temp3").toString();//后天温度String weather1 = weatherday.get("weather1").toString();//今天温度String weather2 = weatherday.get("weather2").toString();//明天温度String weather3 = weatherday.get("weather3").toString();//后天温度cityWeather.setCityname(city);cityWeather.setCity_en(city_en);cityWeather.setCityid(cityid);cityWeather.setDate_y(date_y);cityWeather.setWeek(week);cityWeather.setTempToday(temp1);cityWeather.setTempTommorrow(temp2);cityWeather.setTempAt(temp3);cityWeather.setWeatherToday(weather1);cityWeather.setWeatherTommorrow(weather2);cityWeather.setWeatherAt(weather3);return cityWeather;}return null;}public static void main(String[] args) {GetWeatherService service = new GetWeatherService();CityWeather weatherDay = service.getWeatherDay("山东", "日照");System.out.println("城市名:"+weatherDay.getCityname());System.out.println("城市气温:"+weatherDay.getTempToday());System.out.println("城市天气:"+weatherDay.getWeatherToday());}public Object[] getProvinceArray(){Map provincemap = getProvinceMap();Object[] provinceArray = (Object[]) provincemap.keySet().toArray();return provinceArray;}public Object[] getCityArray(String province){Map citymap = getCityMap(province);Object[] cityArray = (Object[]) citymap.keySet().toArray();return cityArray;}
}

dao层:

public class GetWeatherDao {private String weatherUrl;private String cityUrl;public GetWeatherDao() {this.weatherUrl = "http://m.weather.com.cn/data/";this.cityUrl = "http://flash.weather.com.cn/wmaps/xml/";}/*** 输入被采集城市的城市气象id,返回该城市未来七天气象信息的json串*/public String getWeatherJson(String cityid){StringBuilder builder=new StringBuilder();InputStream is = null;try {String urlstr = weatherUrl + cityid + ".html";System.out.println(urlstr);URL url = new URL(urlstr);is = url.openConnection().getInputStream();byte[] b = new byte[1024];for (int n; (n = is.read(b)) != -1;) {builder.append(new String(b, 0, n,"UTF-8"));}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return builder.toString();}/*** 输入被采集城市以及被采集的城市所在的省份的id,Url地址为默认地址,返回包含有xml文件的流**/public InputStream getCityXml(String provincexml){InputStream is = null;try {/*** 这里该*/String urlstr=cityUrl+provincexml+".xml";URL url=new URL(urlstr);is = url.openConnection().getInputStream();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return is;}
}

存储对象:

public class CityWeather {private String cityname;//城市名private String cityid;//城市天气编号private String city_en;//城市名拼音private String date_y;//日期private String week;//星期private String tempToday;//今天的温度范围private String tempTommorrow;//明天的温度范围private String tempAt;//后天的温度范围private String weatherToday;//今天的天气private String weatherTommorrow;//明天的天气private String weatherAt;//后天的天气public CityWeather() {// TODO Auto-generated constructor stub}public String getCityname() {return cityname;}public void setCityname(String cityname) {this.cityname = cityname;}public String getCityid() {return cityid;}public void setCityid(String cityid) {this.cityid = cityid;}public String getCity_en() {return city_en;}public void setCity_en(String city_en) {this.city_en = city_en;}public String getDate_y() {return date_y;}public void setDate_y(String date_y) {this.date_y = date_y;}public String getWeek() {return week;}public void setWeek(String week) {this.week = week;}public String getTempToday() {return tempToday;}public void setTempToday(String tempToday) {this.tempToday = tempToday;}public String getTempTommorrow() {return tempTommorrow;}public void setTempTommorrow(String tempTommorrow) {this.tempTommorrow = tempTommorrow;}public String getTempAt() {return tempAt;}public void setTempAt(String tempAt) {this.tempAt = tempAt;}public String getWeatherToday() {return weatherToday;}public void setWeatherToday(String weatherToday) {this.weatherToday = weatherToday;}public String getWeatherTommorrow() {return weatherTommorrow;}public void setWeatherTommorrow(String weatherTommorrow) {this.weatherTommorrow = weatherTommorrow;}public String getWeatherAt() {return weatherAt;}public void setWeatherAt(String weatherAt) {this.weatherAt = weatherAt;}}

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了