- 浏览: 663489 次
- 性别:
- 来自: 安徽
文章分类
- 全部博客 (252)
- Html/Div+CSS (12)
- Js/Jquery (34)
- Flex (2)
- Ajax (3)
- Java (35)
- C# (15)
- Spring (16)
- Hibernate (13)
- Struts2 (12)
- Struts1 (7)
- DWR (1)
- iBatis/myBatis (9)
- Tag(JSTL、EL) (1)
- Android (44)
- SQL (7)
- SEO (7)
- Exception (3)
- Tool (10)
- Other (3)
- WebService (9)
- Apache (7)
- Ext (0)
- Utils (12)
- thinking in programme (2)
- Hadoop (0)
- ActiveMQ (0)
- HTML5/CSS3 (0)
- WPF (1)
- NodeJs (1)
- 设计模式 (0)
- 程序人生 (1)
- 随笔 (1)
- Linux (1)
- Load Balance (0)
最新评论
-
drinkjava2:
太复杂了而且不通用,利用ThreadLocal可完美解决这一问 ...
JDBC的多条件动态查询 -
u013107014:
multipartRequest.getFiles(" ...
多文件上传 by MultipartFile and Multiple -
liyys:
可惜没讲你mysql数据库的表的设计
iBatis入门 -
Mapple_leave:
效果还是挺不错的,谢谢了。
中文简体与繁体的转换 -
arcpad:
JS禁用浏览器退格键
随着项目的进行,总感觉以前写的东西不是很如意,或者说没有说到点子上去,比如说上次在“怎么在网站中插入天气预报,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);
- weather.rar (74.4 KB)
- 下载次数: 21
发表评论
-
统一中英文长度问题
2012-07-20 00:17 1896最近因为在做一个项目要求很多都是英文,所以这就涉及到在页 ... -
FireFox默认隐藏无效图片(broken image),而不是像IE中那般,显示一个红X
2012-07-11 00:07 1227直接上代码: <style type="te ... -
IE6,IE7下设置body{overflow:hidden;}失效Bug
2012-04-02 22:46 2644最近做项目发现在IE7下设置body{overflow:h ... -
【转】一些比较高效的CSS写法建议
2012-02-07 22:21 1087当浏览器解析html的时候,它构造了一个文档树来展现所有被显示 ... -
怎么在网站中插入天气预报,qq,发送邮件
2012-01-31 23:06 2651晚上看博客看到一个关于在网站中插入天气预报的,所以在这里收藏一 ... -
Java农历(阴历)工具类
2012-01-20 11:30 2304在真实的项目开发中会可能会遇到项目需要对阴历即我们所说的农历节 ... -
联系人导出(msn、邮箱)
2012-01-16 11:48 8311在项目开发中,不知道大家有没有涉及到导出邮箱或msn的联系人( ... -
Json lib
2012-01-15 10:04 2065Json_lib可以方便的将Java对象转成json格式 ... -
中文简体与繁体的转换
2012-01-15 10:07 7169虽然说中文繁体与简体的转换在项目中使用到的概念很小,但是这也 ... -
根据ip获得城市,根据手机号获得归属地,根据身份证号获得
2012-01-24 09:21 1756今天找到有道的一个api工具,根据ip获得城市,根据手机号获得 ... -
css设置未知尺寸图片的水平和垂直居中
2012-01-14 08:42 1759<!DOCTYPE html PUBLIC &qu ... -
Js/Jquery 实现checkbox全选,反选,全不选
2012-01-12 14:04 8312Use by JS: <!DOCTYPE html P ... -
CSS的ime-mode屏蔽中文输入法
2012-01-18 13:24 5286ime是Input Method Editor的简称。 ... -
css使用中文注释引起失效解决方法
2012-01-23 14:11 1512想必大家在设计页面的时候经常会遇到CSS也的没问题,缺不 ... -
表格 各行换色
2012-01-22 15:04 1324<!DOCTYPE html PUBLIC &qu ... -
js实现限时抢购
2012-01-12 10:44 2007<!DOCTYPE html PUBLIC &qu ... -
CSS 文字换行
2012-01-18 13:23 19761、强制不换行: div { ... -
省市联动 js
2012-01-12 08:44 1523在项目开发中我们可能会遇到需要选择省市的下拉框,上次在简明现代 ... -
汉字转拼音(pinyin4j)
2012-01-11 12:12 2132今天看了一篇博客(http://guyexin-126- ... -
设置IE浏览器文档模式版本
2012-01-11 08:53 5282浏览器版本问题是所有开发web的程序员共同的诟病,尤其是IE浏 ...
相关推荐
调用天气预报webService axis1.4
WebService 的调用天气预报,使用wsimport命令生成webService客户端代码实例,java project 下载后可以直接运行
此文档介绍了Android调用天气预报的WebService简单例子。
asp.net 调用天气预报接口实现天气查寻源码
很多人调用中国天气网天气预报,但是不知道怎么生成令牌key,今天写了个demo分享给大家。
JAVA调用天气预报WEB服务实例。这是一位JAVA高手写的调用webxml.com.cn天气预报Web Service实例。
练习调用webservice调用查看天气,c# winfrom调用
VC++2012 调用 天气预报WebService 源代码,获取指定城市的天气信息 使用了GSOAP工具集,参考http://blog.csdn.net/startexcel/article/details/8208135
c# winform 调用公共天气预报webservice接口,初学者参考,大神忽略 c# winform 调用公共天气预报webservice接口,初学者参考,大神忽略
Android之Webservice详解与调用天气预报Webservice完整实例
Delphi7 天气预报 WebSevice
java调用天气预报接口,利用servlet调用天气预报接口 获得天气预报具体数据,显示在自己的网页中
调用天气预报Web Services的一个实例
使用Qt实现的不规则窗体,实现了按任意png图形确定窗体形状.以及调用WebService实现天气预报和翻译功能.
java调用天气预报webservice 详情:https://blog.csdn.net/sinat_36710456/article/details/80059491
通过webService调用天气预报的小例子。实现读取天气信息的功能。
很简单的android调用天气预报的webservice的小例子,可以对照着博客中的解析进行学习~
界面用Qt,调用天气预报Webservice用gsoap,运行环境window
VC++调用天气预报Web services方法总结
调用天气预报google web java写的。直接在myeclipse里面运行。