我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Java调用yahoo!API获取天气数据-创新互联

先把代码复制上来,以后再做补充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 ID17   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() + "	"
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() + "		经度:"
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() + "		"
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() + "℃		"
54                     + today_map.getNamedItem("text").getNodeValue()
55                     + "		发布时间:"
56                     + today_map.getNamedItem("date").getNodeValue() + "
");
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                         + "	最低气温:" + map.getNamedItem("low").getNodeValue()
64                         + "℃" + "	高气温:"
65                         + map.getNamedItem("high").getNodeValue() + "℃" + "	"
66                         + map.getNamedItem("text").getNodeValue());
67             }69         } catch (Exception e) {
70  // TODO Auto-generated catch block71             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 block83             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 block96             e.printStackTrace();
97         }
98     }
99 }

标题名称:Java调用yahoo!API获取天气数据-创新互联
本文来源:http://mswzjz.cn/article/gggji.html

其他资讯