- 浏览: 446940 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (211)
- java (37)
- spring相关 (3)
- struts (10)
- 面试 (1)
- jsp/servlet (18)
- 持久化框架 (1)
- IT相关新闻 (3)
- 服务器 (11)
- 插件 (4)
- pushlet (3)
- js (24)
- oracle (29)
- mysql (9)
- hibernate (5)
- 开发工具 (6)
- jquery (6)
- 页面标签jstl,el (1)
- linux (25)
- 英语 (1)
- log4j (1)
- html/css (6)
- sqlserver (2)
- dwr (1)
- 设计模式 (4)
- vmware (2)
- office (1)
- eclipse (5)
- svn (1)
- webservice (1)
最新评论
-
18335864773:
建议使用 pageoffice 组件套红
js操作word套红 -
lopez:
数据库系统的客户程序只要向数据库系统声明了一个事务,数据库系统 ...
Hibernate事物控制与管理 -
liujq4512:
删了还是没用
An internal error occurred during: "Initializing Java Tooling". -
elaine0111:
非常感谢这篇文章,嘿嘿,解决了我的问题。我把这段代码保存在我的 ...
Js设置文本框中焦点位置在最后 -
weishuguangeye:
不错!
单例模式(Singleton)
最近工作有用到web services,而这两天也比较有空,就弄了一个获取天气预报的Util。以前以为这有多难,因为数据来源是个困难。现在用web services的技术,这个就不用管了。说共享也好,说偷也好,总之是达到目的了。网上找了几个提供web services的网站。发现下面这个是相当不错的。
http://www.webxml.com.cn/Webservices/WeatherWebService.asmx
下面就用java把具体的代码写写吧!
这里我采用比较简单的get请求调用,毕竟这也没什么秘密可言,就用最简单的就可以了。
还有,这里很多捕获异常的东西给我去掉了,自己加吧!
public final class WeatherUtil{ private static String SERVICES_HOST = "www.webxml.com.cn"; private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/"; private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL + "getSupportCity?byProvinceName=ALL"; private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + "getWeatherbyCityName?theCityName="; private WeatherUtil(){} public static InputStream getSoapInputStream(String url) { InputStream is = null; URL U = new URL(url); URLConnection conn = U.openConnection(); conn.setRequestProperty("Host", SERVICES_HOST); conn.connect(); is = conn.getInputStream(); return is; } //取得支持的城市列表 public static ArrayList<String> getSupportCity() { ArrayList cityList = null; Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(SUPPORT_CITY_URL); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); int len = nl.getLength(); cityList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String city = n.getFirstChild().getNodeValue(); cityList.add(city); } is.close(); return cityList; } //取得城市的天气 public static ArrayList<String> getWeather(String city) { ArrayList weatherList = null; Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder();//这里他的编码格式就是这样,我试了几个也没办法。。只好这样混过去了 InputStream is = getSoapInputStream(WEATHER_QUERY_URL + new String(city.getBytes("UTF-8"), "GBK")); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); int len = nl.getLength(); weatherList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String weather = n.getFirstChild().getNodeValue(); weatherList.add(weather); } is.close(); return weatherList; } public static void main(String[] args) throws Exception { ArrayList<String> weatherList = WeatherUtil.getWeather("59287"); // ArrayList<String> weatherList = WeatherUtil.getSupportCity(); for (String weather : weatherList) { System.out.println(weather); } }}
http://www.webxml.com.cn/Webservices/WeatherWebService.asmx
下面就用java把具体的代码写写吧!
这里我采用比较简单的get请求调用,毕竟这也没什么秘密可言,就用最简单的就可以了。
还有,这里很多捕获异常的东西给我去掉了,自己加吧!
public final class WeatherUtil{ private static String SERVICES_HOST = "www.webxml.com.cn"; private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/"; private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL + "getSupportCity?byProvinceName=ALL"; private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL + "getWeatherbyCityName?theCityName="; private WeatherUtil(){} public static InputStream getSoapInputStream(String url) { InputStream is = null; URL U = new URL(url); URLConnection conn = U.openConnection(); conn.setRequestProperty("Host", SERVICES_HOST); conn.connect(); is = conn.getInputStream(); return is; } //取得支持的城市列表 public static ArrayList<String> getSupportCity() { ArrayList cityList = null; Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(SUPPORT_CITY_URL); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); int len = nl.getLength(); cityList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String city = n.getFirstChild().getNodeValue(); cityList.add(city); } is.close(); return cityList; } //取得城市的天气 public static ArrayList<String> getWeather(String city) { ArrayList weatherList = null; Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder();//这里他的编码格式就是这样,我试了几个也没办法。。只好这样混过去了 InputStream is = getSoapInputStream(WEATHER_QUERY_URL + new String(city.getBytes("UTF-8"), "GBK")); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); int len = nl.getLength(); weatherList = new ArrayList<String>(len); for (int i = 0; i < len; i++) { Node n = nl.item(i); String weather = n.getFirstChild().getNodeValue(); weatherList.add(weather); } is.close(); return weatherList; } public static void main(String[] args) throws Exception { ArrayList<String> weatherList = WeatherUtil.getWeather("59287"); // ArrayList<String> weatherList = WeatherUtil.getSupportCity(); for (String weather : weatherList) { System.out.println(weather); } }}
发表评论
-
java生成pdf以及解决中文中文乱码问题
2013-03-11 16:33 12360itext版本:5.4.0 下载 ... -
jax-ws webservice编程
2013-01-29 16:07 64471.理解JAX-WS 1.1JAX-WS概述 JAX ... -
was6.1修改java编译版本
2013-01-11 09:06 1905jsp中如果用到了jdk1.5的新特性,在was6.1下编译会 ... -
eclipse生成javadoc乱码解决方式
2013-01-11 09:05 807在Extra javadoc options下增加如下参数 j ... -
Eclipse Jee项目开发前准备 (转)
2012-12-31 09:06 978Eclipse Jee项目编码设置 Window-> ... -
SVN 签出源码 Struts Spring Hibernate
2011-09-15 09:38 1149SVN 签出源码 Struts Spring ... -
几种Java数据库连接池实现(一)
2011-08-31 14:15 1641几种Java数据库连接池实现(一) (一) package s ... -
win7下硬盘安装ubuntu
2011-08-22 20:36 959安装方法见附件 -
比较好的java网站[推荐]
2011-04-07 12:42 15621. The Source for Java Technol ... -
jad反编译jar文件
2011-02-15 16:56 1456//首先使用jar命令将jar文件解压缩到classes目录 ... -
接口与抽象类区别
2011-01-19 15:21 611接口与抽象类区别 《设计模式Java手册》中的,觉得写得很简 ... -
正则入门
2010-12-20 13:21 843< type="text/javascript ... -
序列键生成器与单例及多例模式
2010-12-13 10:37 1059在一个关系数据库中,所有的数据都是存储在表里,而每一个表都有一 ... -
Java中日期格式转换
2010-12-10 15:35 1469Java中日期格式转换 /** * 字符串转换为jav ... -
java解压缩zip包
2010-11-10 12:32 2069package test; import java.io.B ... -
项目中常见的错误
2010-09-27 08:47 75759项目中遇到的错误(c9bcf64c) sql错误 找不到 ... -
单例模式(Singleton)
2010-09-25 22:51 1219BestUpon 写道 首先我们先 ... -
Java杂谈(十三)——ORM
2010-08-20 09:02 1064其实J2ee的规范指南 ... -
Java杂谈(十二)--JVM
2010-08-20 09:01 1008本来这次应该讲 ... -
Java杂谈(十一)--Spring
2010-08-20 09:01 865引用《Spring2.0技 ...
相关推荐
vc6工程,可以从网上获取天气预报的信息。城市可以自己设定。 只是调用一个类而已,该类我已经封装好了。核心代码是参考其他人的。
Delphi获取天气预报的源码,比较简单,有客户端可以查看,最早是为了应付客户要求弄的,简单了解一下吧
function getWeatherInfo { $ getScript "... + $ param { city : $ "#position" val 城市 day : 0 password : "DJOYnieT8234jlsK"... } function json { ...weather &qu [更多]
在asp.ne中t使用WebService获取天气预报信息的例子,并且包括常用的免费的Webservice地址,包括天气、列车车次、股票等等!
用python获取中央气象局天气预报,使用re模块
输入城市名称获取天气预报,可做android开发
本工具使用C#语言开发WinFrom软件,根据国家气象局天气预报接口获取未来十五天天气预报。只用来新手学习,里边主要涉及到Url请求,Json转List,Json转model。
自动联网获取天气预报是一个自动联网获取天气信息并且可以在指定时间发送给某个人的小例子源码,其实这个例子非常适合一些情侣或者家人朋友之间使用,如果再稍微扩展一下就是一个不错的例子,例如每天给异地的女友...
描述信息: http://blog.csdn.net/genfeng/article/details/31098021 RESTful学习笔记
根据传入的WSDL地址和调用的方法名及参数值等,自动调用WebService方法生成客户端,服务中的Service.wsdl文件是通过“添加Web引用”而来的文件,该服务提供了所有要查询的省份及相应的城市天气预报信息,如城市今天的...
DELPHI 通过WEBSERVICE获取天气预报,可获取全球,全国各省市天气预报详情
获取天气预报信息,只适合初学者,有一定基础的同学不要下载此源代码
获取天气信息,可以应用在自己的程序中
使用Web Service获取天气预报 使用Web Service获取天气预报
JS实现的 得到weather.cn 的天气内容。 直接访问html文件即可。内有实现省市联动的js,欢迎大家下载。
运用Ajax技术实现自动获取天气预报系统
完整版获取天气预报.rar
NULL 博文链接:https://jiachenmianyu.iteye.com/blog/1409443
c++获取天气 c++调用webSevers 获取天气预报。