Java调用yahoo!API获取天气数据

先把代码复制上来,以后再做补充

 package com.weather.test;

 import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; public class Test {
// Application ID
final String appid = "雅虎Application ID 需申请"; public String getWOEID(String name) throws Exception {
URL url = new URL("http://where.yahooapis.com/v1/places.q(" + name
+ ")?appid=" + appid);
URLConnection connection = url.openConnection();
Document doc = stringToDOM(connection.getInputStream());
Node location = doc.getElementsByTagName("name").item(0);
Node node = doc.getElementsByTagName("woeid").item(0);
String woeid = node.getTextContent();
System.out.println(location.getTextContent() + "\t"
+ node.getNodeName() + ":" + woeid);
return woeid;
} public void getWeather(String woeid) {
try {
URL url = new URL("http://weather.yahooapis.com/forecastrss?u=c&w="
+ woeid);
URLConnection connection = url.openConnection();
Document doc = stringToDOM(connection.getInputStream());
Node lat = doc.getElementsByTagName("geo:lat").item(0);
Node lon = doc.getElementsByTagName("geo:long").item(0);
System.out.println("纬度:" + lat.getTextContent() + "\t\t经度:"
+ lon.getTextContent());
Node node = doc.getElementsByTagName("yweather:astronomy").item(0);
Node sunrise_node = node.getAttributes().item(0);
Node sunset_node = node.getAttributes().item(1);
System.out.println("今天天气情况:");
System.out.println("日出时间:" + sunrise_node.getNodeValue() + "\t\t"
+ "日落时间:" + sunset_node.getTextContent()); NamedNodeMap today_map = doc
.getElementsByTagName("yweather:condition").item(0)
.getAttributes();
System.out.println("温度:"
+ today_map.getNamedItem("temp").getNodeValue() + "℃\t\t"
+ today_map.getNamedItem("text").getNodeValue()
+ "\t\t发布时间:"
+ today_map.getNamedItem("date").getNodeValue() + "\n"); NodeList list = doc.getElementsByTagName("yweather:forecast");
System.out.println("未来五天天气情况:");
for (int i = 0; i < list.getLength(); i++) {
NamedNodeMap map = list.item(i).getAttributes();
System.out.println(map.getNamedItem("day").getNodeValue()
+ "\t最低气温:" + map.getNamedItem("low").getNodeValue()
+ "℃" + "\t最高气温:"
+ map.getNamedItem("high").getNodeValue() + "℃" + "\t"
+ map.getNamedItem("text").getNodeValue());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public Document stringToDOM(InputStream input) {
try {
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = db.parse(input);
return doc;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
} public static void main(String[] args) {
Test test = new Test();
try {
String woeid = test.getWOEID("meizhou");
test.getWeather(woeid);
System.out.println("From yahoo!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上一篇:《算法》第六章部分程序 part 2


下一篇:免费天气API,天气JSON API,不限次数获取十五天的天气预报