Java调用yahoo!API获取天气数据
2013-07-09 22:21
531 查看
先把代码复制上来,以后再做补充
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(); } } }
相关文章推荐
- Java之调用天气API及解析JSON数据
- Java yahoo 雅虎 股票接口获取股票交易数据
- Java获取yahoo天气预报
- OpenDayLight 氢版本 RestAPI 调用实例(1)-拓扑的获取(Java)
- 根据新浪天气API获取各地天气状况(Java实现)
- 通过api使用HttpURLConnection 获取返回天气数据。
- java微信开发API第三步 微信获取以及保存接口调用凭证
- Java调用天气接口(百度天气)解析返回的JSON数据
- Ajax 通过城市名获取数据(全国天气预报API)
- 根据新浪天气API获取各地天气状况(Java实现)
- 使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- java调用webservice获取指定城市天气情况的例子
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java 利用新浪天气API获取天气预报
- eoLinker-API_Shop_天气类API调用的代码示例合集:全国天气预报、实时空气质量数据查询、PM2.5空气质量指数等
- Windows下使用Java调用ElasticSearch提供的相关API进行数据搜索完整实例演示
- Java api 调用Sqoop2进行MySQL-->Hive的数据同步
- Redis和nosql简介,api调用;Redis数据功能(String类型的数据处理);List数据结构(及Java调用处理);Hash数据结构;Set数据结构功能;sortedSet(有序集合)数
- unity3d调用中央气象局城市天气数据,api调用
- zabbix 二次开发之调用api接口获取历史数据