一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java 获取天气预报实现代码

时间:2011-07-24 编辑:简简单单 来源:一聚教程网

最简单的办法是申请一个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
 
public static HashMap cityCode = new HashMap();

/* 初始化城市代号 */
private void initCitys() {
cityCode.put("北京", "0008");
cityCode.put("天津", "0133");
cityCode.put("武汉", "0138");
cityCode.put("杭州", "0044");
cityCode.put("合肥 ", "0448");
cityCode.put("上海 ", "0116");
cityCode.put("福州 ", "0031");
cityCode.put("重庆 ", "0017");
cityCode.put("南昌 ", "0097");
cityCode.put("香港 ", "0049");
cityCode.put("济南 ", "0064");
cityCode.put("澳门 ", "0512");
cityCode.put("郑州 ", "0165");
cityCode.put("呼和浩特 ", "0249");
cityCode.put("乌鲁木齐 ", "0135");
cityCode.put("长沙 ", "0013");
cityCode.put("银川 ", "0259");
cityCode.put("广州 ", "0037");
cityCode.put("拉萨 ", "0080");
cityCode.put("海口 ", "0502");
cityCode.put("南京 ", "0100");
cityCode.put("成都 ", "0016");
cityCode.put("石家庄 ", "0122");
cityCode.put("贵阳 ", "0039");
cityCode.put("太原 ", "0129");
cityCode.put("昆明 ", "0076");
cityCode.put("沈阳 ", "0119");
cityCode.put("西安 ", "0141");
cityCode.put("长春 ", "0010");
cityCode.put("兰州 ", "0079");
cityCode.put("西宁 ", "0236");
}
  

接下来我们要创建链接,以获取天气预报的XML文档
private Document getWeatherXML(String cityCode) throws IOException {
URL url = new URL("http://weather.yahooapis.com/forecastrss?p=CHXX"
+ cityCode + "&u=c");
URLConnection connection = url.openConnection();
Document Doc = stringToElement(connection.getInputStream());
return Doc;
}
  

您也可以选择保存获取的天气XML文档
/* 保存获取的天气信息XML文档 */
private void saveXML(Document Doc, String Path) {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(Doc);
File file = new File(Path);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
} catch (Exception e) {
System.out.println("保存文件出错!");
}
}
  

本人获取的一份XML保存如下
 
-
-
Yahoo! Weather - Wuhan, CH
http://us.rd.yahoo.com/dailynews/rss/weather/Wuhan__CH/*http://weather.yahoo.com/forecast/CHXX0138_c.html
Yahoo! Weather for Wuhan, CH
en-us
Sat, 27 Mar 2010 11:00 pm CST
60





-
Yahoo! Weather
142
18
http://weather.yahoo.com
http://l.yimg.com/a/i/us/nws/th/main_142b.gif

-
Conditions for Wuhan, CH at 11:00 pm CST
30.58
114.27
http://us.rd.yahoo.com/dailynews/rss/weather/Wuhan__CH/*http://weather.yahoo.com/forecast/CHXX0138_c.html
Sat, 27 Mar 2010 11:00 pm CST

-
-



CHXX0138_2010_03_27_23_00_CST



-
  

好啦,下面就是解析XML了,您看一下XML文档,如果您了解的话很容易获取其中的信息
/* 获取天气信息 */
public String getWeather(String city) {
String result = null;
try {
Document doc = getWeatherXML(GetWeatherInfo.cityCode.get(city));
NodeList nodeList = doc.getElementsByTagName("channel");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NodeList nodeList1 = node.getChildNodes();
for (int j = 0; j < nodeList1.getLength(); j++) {
Node node1 = nodeList1.item(j);
if (node1.getNodeName().equalsIgnoreCase("item")) {
NodeList nodeList2 = node1.getChildNodes();
for (int k = 0; k < nodeList2.getLength(); k++) {
Node node2 = nodeList2.item(k);
if (node2.getNodeName().equalsIgnoreCase(
"yweather:forecast")) {
NamedNodeMap nodeMap = node2.getAttributes();
Node lowNode = nodeMap.getNamedItem("low");
Node highNode = nodeMap.getNamedItem("high");
Node codeNode = nodeMap.getNamedItem("code");
String day = "今天";
if (result == null) {
result = "";
} else {
day = "n明天";
}
result = result
+ day
+ " "
+ dictionaryStrings[Integer
.parseInt(codeNode
.getNodeValue())]
+ "t最低温度:" + lowNode.getNodeValue()
+ "℃ t最高温度:" + highNode.getNodeValue()
+ "℃ ";
}
}
}
}
}
saveXML(doc, "C:UsersyguiDesktopWeather.xml");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
  

整个程序的代码如下:
package stwolf.hustbaidu.java.learn.filelist;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
class GetWeatherInfo {
public static HashMap cityCode = new HashMap();
private final String[] dictionaryStrings = { "龙卷风", "热带风暴", "飓风", "强雷阵雨",
"雷阵雨", "小雨加雪", "雨加冰雹", "雪加冰雹", "冰雨", "毛毛雨", "冻雨", "阵雨", "阵雨", "小雪",
"零星小雪", "高吹雪", "雪", "冰雹", "雨夹雪", "尘", "雾", "薄雾", "多烟的", "大风", "有风",
"寒冷", "阴天", "夜间阴天", "白天阴天", "夜间多云", "白天多云", "夜间清亮", "晴朗", "转晴",
"转晴", "雨夹冰雹", "热", "雷阵雨", "雷阵雨", "雷阵雨", "雷阵雨", "大雪", "阵雪", "大雪",
"多云", "雷", "阵雪", "雷雨" };
public GetWeatherInfo() {
initCitys();
}
/* 初始化城市代号 */
private void initCitys() {
cityCode.put("北京", "0008");
cityCode.put("天津", "0133");
cityCode.put("武汉", "0138");
cityCode.put("杭州", "0044");
cityCode.put("合肥 ", "0448");
cityCode.put("上海 ", "0116");
cityCode.put("福州 ", "0031");
cityCode.put("重庆 ", "0017");
cityCode.put("南昌 ", "0097");
cityCode.put("香港 ", "0049");
cityCode.put("济南 ", "0064");
cityCode.put("澳门 ", "0512");
cityCode.put("郑州 ", "0165");
cityCode.put("呼和浩特 ", "0249");
cityCode.put("乌鲁木齐 ", "0135");
cityCode.put("长沙 ", "0013");
cityCode.put("银川 ", "0259");
cityCode.put("广州 ", "0037");
cityCode.put("拉萨 ", "0080");
cityCode.put("海口 ", "0502");
cityCode.put("南京 ", "0100");
cityCode.put("成都 ", "0016");
cityCode.put("石家庄 ", "0122");
cityCode.put("贵阳 ", "0039");
cityCode.put("太原 ", "0129");
cityCode.put("昆明 ", "0076");
cityCode.put("沈阳 ", "0119");
cityCode.put("西安 ", "0141");
cityCode.put("长春 ", "0010");
cityCode.put("兰州 ", "0079");
cityCode.put("西宁 ", "0236");
}
private Document getWeatherXML(String cityCode) throws IOException {
URL url = new URL("http://weather.yahooapis.com/forecastrss?p=CHXX"
+ cityCode + "&u=c");
URLConnection connection = url.openConnection();
Document Doc = stringToElement(connection.getInputStream());
return Doc;
}
/* 保存获取的天气信息XML文档 */
private void saveXML(Document Doc, String Path) {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(Doc);
File file = new File(Path);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
} catch (Exception e) {
System.out.println("保存文件出错!");
}
}
/* 获取天气信息 */
public String getWeather(String city) {
String result = null;
try {
Document doc = getWeatherXML(GetWeatherInfo.cityCode.get(city));
NodeList nodeList = doc.getElementsByTagName("channel");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
NodeList nodeList1 = node.getChildNodes();
for (int j = 0; j < nodeList1.getLength(); j++) {
Node node1 = nodeList1.item(j);
if (node1.getNodeName().equalsIgnoreCase("item")) {
NodeList nodeList2 = node1.getChildNodes();
for (int k = 0; k < nodeList2.getLength(); k++) {
Node node2 = nodeList2.item(k);
if (node2.getNodeName().equalsIgnoreCase(
"yweather:forecast")) {
NamedNodeMap nodeMap = node2.getAttributes();
Node lowNode = nodeMap.getNamedItem("low");
Node highNode = nodeMap.getNamedItem("high");
Node codeNode = nodeMap.getNamedItem("code");
String day = "今天";
if (result == null) {
result = "";
} else {
day = "n明天";
}
result = result
+ day
+ " "
+ dictionaryStrings[Integer
.parseInt(codeNode
.getNodeValue())]
+ "t最低温度:" + lowNode.getNodeValue()
+ "℃ t最高温度:" + highNode.getNodeValue()
+ "℃ ";
}
}
}
}
}
saveXML(doc, "C:UsersyguiDesktopWeather.xml");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public Document stringToElement(InputStream input) {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = db.parse(input);
return doc;
} catch (Exception e) {
return null;
}
}
}
public class Test {
public static void main(String arg[]) {
GetWeatherInfo info = new GetWeatherInfo();
String weather = info.getWeather("武汉");
System.out.println(weather);
}
}

热门栏目