购买服务 会员注册 会员登录
  设为首页 加入收藏 联系我们
Welcome to WebXml.com.cn
Web Services 网站建设 域名交易 服务支持 下载中心 信息资讯 联系我们 返回首页
 
服务支持 - Web Services

JAVA Webservices 实现天气预报

www.WebXml.com.cn 发表日期:2008年08月14日 12:10 星期四
 

 

最近工作有用到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);
}
}
}



文章来源 mccxj
鼠中无大将的博客
, 文章地址 http://mccxj.javaeye.com/blog/144161

 
 
 
关闭本窗口
 
请就此篇文章提供反馈:
很有帮助 没有帮助
9 8 7 6 5 4 3 2 1  
   
 
发表评论:
   
< 300中文字或< 600英文字符
 
WebXml.com.cn 声明:
  WebXml.com.cn 和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性(不包括在单独的书面协议项下 WebXml.com.cn 向您提供的任何服务),不作任何声明。所有该等文件及有关图形均 " 依样 " 提供,而不带任何性质的保证。也许这些文件已经过期,且 WebXml.com.cn 不承诺更新它们。WebXml.com.cn 和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于使用、销售、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,WebXml.com.cn 和/或其各供应商就任何问题而造成的损失不负任何责任。
 
 
 
WEB 服务 网站建设 域名交易 服务支持 下载中心 信息资讯 联系我们 首 页
版权所有 © 2006 - 2021 上海思集信息科技有限公司 保留所有权利        本站声明 Search powered by Google
Powered Asp.net 2.0