调用天气预报
随着项目的进行,总感觉以前写的东西不是很如意,或者说没有说到点子上去,比如说上次在“怎么在网站中插入天气预报,qq,发送邮件 ”,只是简单笼统的说了一下,并没有结合项目情况,现在假定项目中需要获取天气预报的信息而界面展示由程序员自己来设设计,ok,这里我们还是使用中国天气 这里提供的插件。
下面我们在浏览器中输入:http://m.weather.com.cn/data/101221001.html,来查看显示的信息:
{"weatherinfo":{"city":"黄山","city_en":"huangshan","date_y":"2012年11月26日","date":"","week":"星期一","fchh":"11","cityid":"101221001","temp1":"11℃~2℃","temp2":"13℃~7℃","temp3":"13℃~9℃","temp4":"10℃~8℃","temp5":"13℃~8℃","temp6":"11℃~8℃","tempF1":"51.8℉~35.6℉","tempF2":"55.4℉~44.6℉","tempF3":"55.4℉~48.2℉","tempF4":"50℉~46.4℉","tempF5":"55.4℉~46.4℉","tempF6":"51.8℉~46.4℉","weather1":"多云转晴","weather2":"阴转小雨","weather3":"小雨","weather4":"阴","weather5":"小雨","weather6":"中雨转小雨","img1":"1","img2":"0","img3":"2","img4":"7","img5":"7","img6":"99","img7":"2","img8":"99","img9":"7","img10":"99","img11":"8","img12":"7","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":"西北风转东风小于3级","wind2":"东南风小于3级","wind3":"东北风小于3级","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"西北风","fx2":"东风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"凉","index_d":"天气凉,建议着厚外套加毛衣等春秋服装。年老体弱者宜着大衣、呢外套加羊毛衫。","index48":"温凉","index48_d":"建议着夹衣或西服套装加薄羊毛衫等春秋服装。年老体弱者宜着夹衣或风衣加羊毛衫。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适宜","index_co":"较舒适","st1":"11","st2":"0","st3":"14","st4":"8","st5":"13","st6":"10","index_cl":"较适宜","index_ls":"基本适宜","index_ag":"极不易发"}}
这里我们可以发现,返回的是一个JSON的字符串,那么我们可以通过定义实体类,通过GSON or json lib 来将JSON字符串转实体,然后将实体传到前台显示。
ok,下面我们再来理一下整体思路:
1、上面http://m.weather.com.cn/data/101221001.html,这个地址因不同的城市而编码不同,所以我们需要根据提供的xml,通过name查找对应的编码,然后设置路径(这里需要涉及XML的解析)
2、封装实体,将JSON转为对象
3、将对象传到前台通过我们自己定义的样式展示出来,废话说完了,上代码
一、根据不同城市获取不同的编码
City.java
package com.iflytek.demo;
/**
* @author xdwang
*
* @create 2012-11-26 下午1:30:17
*
* @email:xdwangiflytek@gmail.com
*
* @description 城市实体类
*
*/
public class City {
/**
* 城市名称
*/
private String name;
/**
* 城市对应的编码
*/
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
CitySaxHelper.java
package com.iflytek.demo;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* @author xdwang
*
* @create 2012-11-26 下午1:33:37
*
* @email:xdwangiflytek@gmail.com
*
* @description XMLTO City by sax
*
*/
public class CitySaxHelper extends DefaultHandler {
private List<City> cities = null; // 保存多条数据
private City city = null;
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("city".equals(qName)) {
this.cities.add(this.city);
this.city = null; // 准备保存下次的数据
}
}
@Override
public void startDocument() throws SAXException {
this.cities = new ArrayList<City>(); // 表示开始解析文档,所以要设置集合
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("city".equals(qName)) { // 是一个city节点
this.city = new City(); // 实例化city对象
}
for (int i = 0; i < attributes.getLength(); i++) {
if (attributes.getLocalName(i) != null && attributes.getLocalName(i) != "" && attributes.getValue(i) != null && attributes.getValue(i) != "") {
if ("name".equals(attributes.getLocalName(i))) {
this.city.setName(attributes.getValue(i));
}
if ("code".equals(attributes.getLocalName(i))) {
this.city.setCode(attributes.getValue(i));
}
}
}
}
public List<City> getAll() {
return cities;
}
/**
* @descrption 根据城市获取城市的编码 注意这里的黄山没有,只有黄山区、黄山站,其他可能还会存在相同情况,这里就不特殊处理了
* @author xdwang
* @create 2012-11-26下午2:54:36
* @param CityName
* @return
*/
public String getCodeByCityName(String CityName) {
CityName = CityName.trim();
if (CityName == null || CityName == "") {
return null;
}
for (City city : cities) {
if (city.getName().equals(CityName)) {
return city.getCode();
}
}
return null;
}
}
WeatherInfo.java
package org.elongcom.common;
/**
* @author xdwang
*
* @create 2012-11-25 下午2:32:01
*
* @email:xdwangiflytek@gmail.com
*
* @description 天气实体类
*
*/
public class WeatherInfo {
/**
* 当天温度
*/
private String temp1;
/**
* 第二天温度
*/
private String temp2;
/**
* 当天天气,多云
*/
private String weather1;
/**
* 第二天天气,多云
*/
private String weather2;
/**
* 当天的天气图片
*/
private String img1;
/**
* 第二天的天气图片
*/
private String img2;
/**
* 当天的风向
*/
private String wind1;
/**
* 第二天的风向
*/
private String wind2;
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getTemp2() {
return temp2;
}
public void setTemp2(String temp2) {
this.temp2 = temp2;
}
public String getWeather1() {
return weather1;
}
public void setWeather1(String weather1) {
this.weather1 = weather1;
}
public String getWeather2() {
return weather2;
}
public void setWeather2(String weather2) {
this.weather2 = weather2;
}
public String getImg1() {
return img1;
}
public void setImg1(String img1) {
this.img1 = img1;
}
public String getImg2() {
return img2;
}
public void setImg2(String img2) {
this.img2 = img2;
}
public String getWind1() {
return wind1;
}
public void setWind1(String wind1) {
this.wind1 = wind1;
}
public String getWind2() {
return wind2;
}
public void setWind2(String wind2) {
this.wind2 = wind2;
}
}
通过网页上展示的JSON字符串可以发现这里需要两个对象,WeatherInfoTemp.java
package org.elongcom.common;
/**
* @author xdwang
*
* @create 2012-11-25 下午3:06:41
*
* @email:xdwangiflytek@gmail.com
*
* @description
*
*/
public class WeatherInfoTemp {
private WeatherInfo weatherinfo;
public WeatherInfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
读取网页内容,WeatherHelper.java
package org.elongcom.common;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author xdwang
*
* @create 2012-11-25 下午2:51:23
*
* @email:xdwangiflytek@gmail.com
*
* @description 天气帮助类
*
*/
public class WeatherHelper {
public static String getWeatherInfoStr(String urlPath) {
if (urlPath == null || urlPath == "") {
return "";
}
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(urlPath);
HttpURLConnection hConnect = (HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(hConnect.getInputStream(),"UTF-8"));//这里需要指定编码格式,否则可能会出现乱码
int ch;
for (int length = 0; (ch = rd.read()) > -1; length++)
sb.append((char) ch);
rd.close();
hConnect.disconnect();
} catch (Exception e) {
}
return sb.toString();
}
}
Controller
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = null;
CitySaxHelper citySaxHelper = new CitySaxHelper();
saxParser = factory.newSAXParser();
File file = new File("D:" + File.separator + "WeatherCity.Xml");
saxParser.parse(file, citySaxHelper);
String code = citySaxHelper.getCodeByCityName("黄山区");
String weatherInfoStr = WeatherHelper.getWeatherInfoStr("http://m.weather.com.cn/data/"+code+".html");
Gson gson = new Gson();
WeatherInfoTemp temp = gson.fromJson(weatherInfoStr, WeatherInfoTemp.class);
WeatherInfo weatherInfo = temp.getWeatherinfo();
model.addAttribute("weather", weatherInfo);