小项目算是用三层结构来构架,最上面一层就不写了,直接在service层测试。
中央气象台的接口标准文档我没找到,所以也是参照了别人的应用自己写了一个。。
目录结构为:
这个是根节点
http://flash.weather.com.cn/wmaps/xml/china.xml
返回的表的结构式一定的,所以我直接配置到了本地
如下:
province.xml
<china dn="day">
<city quName="黑龙江" pyName="heilongjiang" cityname="哈尔滨" state1="21" state2="7" stateDetailed="小到中雨转小雨" tem1="21" tem2="13" windState="东风4-5级转3-4级"/>
<city quName="吉林" pyName="jilin" cityname="长春" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="19" tem2="14" windState="东南风3-4级"/>
<city quName="辽宁" pyName="liaoning" cityname="沈阳" state1="8" state2="9" stateDetailed="中雨转大雨" tem1="21" tem2="18" windState="东南风小于3级转3-4级"/>
<city quName="海南" pyName="hainan" cityname="海口" state1="4" state2="1" stateDetailed="雷阵雨转多云" tem1="32" tem2="25" windState="东南风3-4级"/>
<city quName="内蒙古" pyName="neimenggu" cityname="呼和浩特" state1="0" state2="0" stateDetailed="晴" tem1="24" tem2="12" windState="西南风转西北风3-4级"/>
<city quName="新疆" pyName="xinjiang" cityname="乌鲁木齐" state1="0" state2="0" stateDetailed="晴" tem1="29" tem2="17" windState="微风"/>
<city quName="西藏" pyName="xizang" cityname="拉萨" state1="3" state2="7" stateDetailed="阵雨转小雨" tem1="22" tem2="11" windState="微风"/>
<city quName="青海" pyName="qinghai" cityname="西宁" state1="3" state2="3" stateDetailed="阵雨" tem1="20" tem2="10" windState="微风"/>
<city quName="宁夏" pyName="ningxia" cityname="银川" state1="2" state2="1" stateDetailed="阴转多云" tem1="27" tem2="14" windState="北风3-4级转微风"/>
<city quName="甘肃" pyName="gansu" cityname="兰州" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="25" tem2="15" windState="微风"/>
<city quName="河北" pyName="hebei" cityname="石家庄" state1="7" state2="1" stateDetailed="小雨转多云" tem1="30" tem2="22" windState="北风小于3级"/>
<city quName="河南" pyName="henan" cityname="郑州" state1="4" state2="2" stateDetailed="雷阵雨转阴" tem1="31" tem2="24" windState="微风"/>
<city quName="湖北" pyName="hubei" cityname="武汉" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="35" tem2="26" windState="微风"/>
<city quName="湖南" pyName="hunan" cityname="长沙" state1="0" state2="0" stateDetailed="晴" tem1="36" tem2="28" windState="南风小于3级"/>
<city quName="山东" pyName="shandong" cityname="济南" state1="9" state2="3" stateDetailed="大雨转阵雨" tem1="30" tem2="21" windState="南风3-4级转北风小于3级"/>
<city quName="江苏" pyName="jiangsu" cityname="南京" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="27" windState="南风3-4级"/>
<city quName="安徽" pyName="anhui" cityname="合肥" state1="0" state2="1" stateDetailed="晴转多云" tem1="35" tem2="27" windState="西南风3-4级"/>
<city quName="山西" pyName="shanxi" cityname="太原" state1="1" state2="0" stateDetailed="多云转晴" tem1="27" tem2="16" windState="微风转西北风小于3级"/>
<city quName="陕西" pyName="sanxi" cityname="西安" state1="21" state2="1" stateDetailed="小到中雨转多云" tem1="25" tem2="20" windState="西风小于3级"/>
<city quName="四川" pyName="sichuan" cityname="成都" state1="22" state2="22" stateDetailed="中到大雨" tem1="28" tem2="23" windState="北风3-4级"/>
<city quName="云南" pyName="yunnan" cityname="昆明" state1="1" state2="1" stateDetailed="多云" tem1="27" tem2="17" windState="微风"/>
<city quName="贵州" pyName="guizhou" cityname="贵阳" state1="1" state2="1" stateDetailed="多云" tem1="29" tem2="21" windState="南风小于3级"/>
<city quName="浙江" pyName="zhejiang" cityname="杭州" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="26" windState="东风小于3级"/>
<city quName="福建" pyName="fujian" cityname="福州" state1="1" state2="1" stateDetailed="多云" tem1="36" tem2="26" windState="微风"/>
<city quName="江西" pyName="jiangxi" cityname="南昌" state1="0" state2="0" stateDetailed="晴" tem1="35" tem2="27" windState="微风"/>
<city quName="广东" pyName="guangdong" cityname="广州" state1="0" state2="0" stateDetailed="晴" tem1="35" tem2="25" windState="微风"/>
<city quName="广西" pyName="guangxi" cityname="南宁" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="25" windState="南风小于3级"/>
<city quName="北京" pyName="beijing" cityname="北京" state1="7" state2="1" stateDetailed="小雨转多云" tem1="28" tem2="22" windState="微风"/>
<city quName="天津" pyName="tianjin" cityname="天津" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="28" tem2="23" windState="南风转西北风3-4级"/>
<city quName="上海" pyName="shanghai" cityname="上海" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="28" windState="南风3-4级"/>
<city quName="重庆" pyName="chongqing" cityname="重庆" state1="1" state2="8" stateDetailed="多云转中雨" tem1="36" tem2="23" windState="微风"/>
<city quName="香港" pyName="xianggang" cityname="香港" state1="1" state2="1" stateDetailed="多云" tem1="33" tem2="27" windState="微风"/>
<city quName="澳门" pyName="aomen" cityname="澳门" state1="1" state2="1" stateDetailed="多云" tem1="34" tem2="27" windState="微风"/>
<city quName="台湾" pyName="taiwan" cityname="台北" state1="7" state2="10" stateDetailed="小雨转暴雨" tem1="32" tem2="27" windState="微风"/>
<city quName="西沙" pyName="xisha" cityname="西沙" state1="8" state2="8" stateDetailed="中雨" tem1="31" tem2="26" windState="东北风转西风5-6级"/>
<city quName="南沙" pyName="nanshadao" cityname="南沙" state1="4" state2="4" stateDetailed="雷阵雨" tem1="32" tem2="27" windState="西南风5-6级"/>
<city quName="钓鱼岛" pyName="diaoyudao" cityname="钓鱼岛" state1="2" state2="1" stateDetailed="阴转多云" tem1="28" tem2="26" windState="东风4-5级转东南风5-6级"/>
</china>
第二层是访问省级节点:例如:
http://flash.weather.com.cn/wmaps/xml/beijing.xml
返回:
<beijing dn="day"><city cityX="232" cityY="190.8" cityname="延庆" centername="延庆" fontColor="FFFFFF" pyName="yanqing" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="25" tem2="14" temNow="19" windState="微风" windDir="北风" windPower="1级" humidity="70%" time="10:35" url="101010800"/><city cityX="394" cityY="157" cityname="密云" centername="密云" fontColor="FFFFFF" pyName="miyun" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="15" temNow="24" windState="微风" windDir="北风" windPower="2级" humidity="58%" time="10:35" url="101011300"/><city cityX="332" cityY="142" cityname="怀柔" centername="怀柔" fontColor="FFFFFF" pyName="huairou" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="16" temNow="24" windState="微风" windDir="东风" windPower="1级" humidity="56%" time="10:35" url="101010500"/><city cityX="261" cityY="248" cityname="昌平" centername="昌平" fontColor="FFFFFF" pyName="changping" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="25" windState="微风" windDir="东风" windPower="1级" humidity="65%" time="10:45" url="101010700"/><city cityX="439" cityY="232" cityname="平谷" centername="平谷" fontColor="FFFFFF" pyName="pinggu" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="17" temNow="25" windState="微风" windDir="西南风" windPower="1级" humidity="65%" time="10:35" url="101011500"/><city cityX="360" cityY="265" cityname="顺义" centername="顺义" fontColor="FFFFFF" pyName="shunyi" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="西北风" windPower="1级" humidity="65%" time="10:45" url="101010400"/><city cityX="167" cityY="317.65" cityname="门头沟" centername="门头沟" fontColor="FFFFFF" pyName="mentougou" state1="3" state2="3" stateDetailed="阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东南风" windPower="1级" humidity="70%" time="10:45" url="101011400"/><city cityX="264.5" cityY="300.3" cityname="海淀" centername="海淀" fontColor="FFFFFF" pyName="haidian" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="67%" time="10:35" url="101010200"/><city cityX="344.3" cityY="317.65" cityname="朝阳" centername="朝阳" fontColor="FFFFFF" pyName="chaoyang" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="67%" time="10:35" url="101010300"/><city cityX="255" cityY="341.5" cityname="石景山" centername="石景山" fontColor="FFFFFF" pyName="shijingshan" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="67%" time="10:35" url="101011000"/><city cityX="310.05" cityY="339.3" cityname="市中心" centername="市中心" fontColor="FFFF00" pyName="shizhongxin" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="67%" time="10:35" url="101010100"/><city cityX="282.95" cityY="361" cityname="丰台" centername="丰台" fontColor="FFFFFF" pyName="fengtai" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="67%" time="10:35" url="101010900"/><city cityX="198" cityY="386" cityname="房山" centername="房山" fontColor="FFFFFF" pyName="fangshan" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="2级" humidity="66%" time="10:45" url="101011200"/><city cityX="319" cityY="400" cityname="大兴" centername="大兴" fontColor="FFFFFF" pyName="daxing" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="18" temNow="24" windState="微风" windDir="东北风" windPower="1级" humidity="62%" time="10:45" url="101011100"/><city cityX="374" cityY="355" cityname="通州" centername="通州" fontColor="FFFFFF" pyName="tongzhou" state1="1" state2="3" stateDetailed="多云转阵雨" tem1="27" tem2="19" temNow="24" windState="微风" windDir="北风" windPower="2级" humidity="68%" time="10:45" url="101010600"/></beijing>
第三层是访问市级节点:例如:
http://m.weather.com.cn/data/101010100.html
返回值:
{"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":"易发"}}
serveiec层:
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){
//首先,获取所有省份名和拼音的map
Map<String, String> provincemap = getProvinceMap();
//根据所有的省份名 获取 所需要的省份名拼音
String provincexml = provincemap.get(province);
if(provincexml==null){
//有问题,省份不存在
return null;
}
//其次,输入值省份名拼音,获取指定市的城市气象id
Map<String, String> 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<String, String> getProvinceMap() {
Map<String, String> proMap = new HashMap<String, String>();
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<String, String> getCityMap(String provincexml) {
InputStream cityXml = dao.getCityXml(provincexml);
Map<String,String> 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<String, String> provincemap = getProvinceMap();
Object[] provinceArray = (Object[]) provincemap.keySet().toArray();
return provinceArray;
}
public Object[] getCityArray(String province){
Map<String, String> 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 block
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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 block
e.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;
}
}