您的位置:首页 > 编程语言 > Java开发

Java学习之利用yahoo weather api 获取天气预报

2011-07-24 16:47 549 查看
在cnblogs看到 狼の禅 写的Java获取yahoo天气预报一文,于是有了这篇文章。
狼の禅 是将<地点代码,地名> 对用HashMap 来保存在类中的,发现手动查找地点代码然后要一个个加进去还真麻烦,于是想下有没有偷懒的办法。果然,在雅虎 geoplanet 找到了答案。
最简单的办法是申请一个yahoo dev 的key ,然后就可以通过 GeoPlanet api 来查询相应地点的WOEID了。
狼の禅 用的是旧的p 参数传递地点代码,不过最新的api文档里面只对w参数作了说明,因此这里我就用WOEID了。
不想注册申请key,还是自己折腾吧。
下载最新的 GeoPlanet Data ,解压出来有这些个文件:

geoplanet_places_[version].tsv: the WOEID, the placename, and the WOEID of its parent entity
geoplanet_aliases_[version].tsv: alternate names in multiple languages indexed against the WOEID
geoplanet_adjacencies_[version].tsv: the entities neighboring each WOEID
geoplanet_changes_[version].tsv: the list of removed WOEIDs and their replacement WOEID mappings

这里我只取 geoplanet_places_7.6.0.tsv ,用EmEditor 打开,把 中国 地区的 COPY 出到另外一个文件Chinaplaces.tsv .
这个tsv 文件是用tab分隔字段的,places文件的字段有:

WOE_ID ISO Name Language PlaceType Parent_ID

View Code

package com.ihacklog.java.learn;

import com.ihacklog.java.learn.GetWeatherInfo;

/**
* @author HuangYe <荒野无灯>
* @URL http://ihacklog.com */
public class getWeather {
public static void main(String arg[]) {

// OK,let's begin the test.
// Have fun!
GetWeatherInfo info = new GetWeatherInfo();
String weather = info.getWeather("岳阳市");
System.out.println(weather);

weather = info.getWeather("哈尔滨");
System.out.println(weather);

weather = info.getWeather("海口市");
System.out.println(weather);

weather = info.getWeather("长沙市");
System.out.println(weather);

weather = info.getWeather("武汉市");
System.out.println(weather);
}
}




本文参考文档:

Java获取yahoo天气预报

最初发布在:

http://ihacklog.com/?p=4684
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: