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

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

 1 package com.weather.test;
 2 
 3 import java.io.InputStream;
 4 import java.net.URL;
 5 import java.net.URLConnection;
 6 
 7 import javax.xml.parsers.DocumentBuilder;
 8 import javax.xml.parsers.DocumentBuilderFactory;
 9 
10 import org.w3c.dom.Document;
11 import org.w3c.dom.NamedNodeMap;
12 import org.w3c.dom.Node;
13 import org.w3c.dom.NodeList;
14 
15 public class Test {
16     // Application ID
17     final String appid = "雅虎Application ID 需申请";
18 
19     public String getWOEID(String name) throws Exception {
20         URL url = new URL("http://where.yahooapis.com/v1/places.q(" + name
21                 + ")?appid=" + appid);
22         URLConnection connection = url.openConnection();
23         Document doc = stringToDOM(connection.getInputStream());
24         Node location = doc.getElementsByTagName("name").item(0);
25         Node node = doc.getElementsByTagName("woeid").item(0);
26         String woeid = node.getTextContent();
27         System.out.println(location.getTextContent() + "\t"
28                 + node.getNodeName() + ":" + woeid);
29         return woeid;
30     }
31 
32     public void getWeather(String woeid) {
33         try {
34             URL url = new URL("http://weather.yahooapis.com/forecastrss?u=c&w="
35                     + woeid);
36             URLConnection connection = url.openConnection();
37             Document doc = stringToDOM(connection.getInputStream());
38             Node lat = doc.getElementsByTagName("geo:lat").item(0);
39             Node lon = doc.getElementsByTagName("geo:long").item(0);
40             System.out.println("纬度:" + lat.getTextContent() + "\t\t经度:"
41                     + lon.getTextContent());
42             Node node = doc.getElementsByTagName("yweather:astronomy").item(0);
43             Node sunrise_node = node.getAttributes().item(0);
44             Node sunset_node = node.getAttributes().item(1);
45             System.out.println("今天天气情况:");
46             System.out.println("日出时间:" + sunrise_node.getNodeValue() + "\t\t"
47                     + "日落时间:" + sunset_node.getTextContent());
48 
49             NamedNodeMap today_map = doc
50                     .getElementsByTagName("yweather:condition").item(0)
51                     .getAttributes();
52             System.out.println("温度:"
53                     + today_map.getNamedItem("temp").getNodeValue() + "℃\t\t"
54                     + today_map.getNamedItem("text").getNodeValue()
55                     + "\t\t发布时间:"
56                     + today_map.getNamedItem("date").getNodeValue() + "\n");
57 
58             NodeList list = doc.getElementsByTagName("yweather:forecast");
59             System.out.println("未来五天天气情况:");
60             for (int i = 0; i < list.getLength(); i++) {
61                 NamedNodeMap map = list.item(i).getAttributes();
62                 System.out.println(map.getNamedItem("day").getNodeValue()
63                         + "\t最低气温:" + map.getNamedItem("low").getNodeValue()
64                         + "℃" + "\t最高气温:"
65                         + map.getNamedItem("high").getNodeValue() + "℃" + "\t"
66                         + map.getNamedItem("text").getNodeValue());
67             }
69         } catch (Exception e) {
70             // TODO Auto-generated catch block
71             e.printStackTrace();
72         }
73     }
74 
75     public Document stringToDOM(InputStream input) {
76         try {
77             DocumentBuilder db = DocumentBuilderFactory.newInstance()
78                     .newDocumentBuilder();
79             Document doc = db.parse(input);
80             return doc;
81         } catch (Exception e) {
82             // TODO Auto-generated catch block
83             e.printStackTrace();
84             return null;
85         }
86     }
87 
88     public static void main(String[] args) {
89         Test test = new Test();
90         try {
91             String woeid = test.getWOEID("meizhou");
92             test.getWeather(woeid);
93             System.out.println("From yahoo!");
94         } catch (Exception e) {
95             // TODO Auto-generated catch block
96             e.printStackTrace();
97         }
98     }
99 }

 

posted @ 2013-07-09 22:21  Catcher_8  阅读(1164)  评论(0编辑  收藏  举报