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

 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调用yahoo!API获取天气数据的更多相关文章

  1. 高德地图API获取天气

    1.建立行政区规划清单表 use edw; drop table if exists dim_prov_city_adcode; create table if not exists dim_prov ...

  2. 调用ZoomEye API获取信息

    最近在提高自己编程能力,拿一些实用的小工具练下.该脚本为python语言,主要涉及模块urllib,json,os模块. 功能:调用ZoomEye API获取信息 import urllib.requ ...

  3. Java通过webservice接口获取天气信息

    通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...

  4. Android访问中央气象台的天气预报API得到天气数据

      最新说明:该接口已失效! 2014-03-04 可申请它公布的API,需申请:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml 在用A ...

  5. Java 调用 Hbase API 访问接口实现方案

    HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件 ...

  6. Java 调用Restful API接口的几种方式--HTTPS

    摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...

  7. Java 调用 Rest api 设置经典 Linux 虚拟机的实例启停

    现象描述 用户可以通过 Rest API 设置经典 Linux 虚拟机实例的启停.在调用该 API 时需要通过 Azure Active Directory(下文简称 AAD) 获取 Token,但是 ...

  8. java解析xml实例——获取天气信息

    获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...

  9. 使用Python调用Flickr API抓取图片数据

    Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...

随机推荐

  1. char和QChar(Unicode的编码与内存里的值还不是一回事)

    char类型是c/c++中内置的类型,描述了1个字节的内存信息的解析.比如: char gemfield=’g’; 那么在由gemfield标记的这块内存的大小就是1个字节,信息就是01100111, ...

  2. 函数 xdes_get_state

    得到XDES Entry中状态 /**********************************************************************//** Gets the ...

  3. bzoj1084

    乍一看这题好难,根本不会: 直到看到1<=m<=2…… 显然分类讨论dp, 很快想到这题的dp和poj2430相类似 m=2的时候f[i,j,k]表示到第i行用了j个矩阵结尾状态为k时最大 ...

  4. DMG提取安装文件

    打开原版的DMG文件,选中安装文件,按下鼠标右键,选择显示包内容 <ignore_js_op> 按照下图的路径,一直找到installESD.dmg,双击打开它<ignore_js_ ...

  5. 如何在 Windows 7 安裝 SharePoint Server 2010

    转:http://support.microsoft.com/kb/2683572/zh-tw 關於作者: 本文由微軟最有價值專家 MVP 歐志信 提供.微軟十分感謝 MVP 主動地將他們的經驗與上百 ...

  6. SharePoint 2010 WSP包部署过程中究竟发生什么?

    转:http://www.xue163.com/158/6/1585365.html 在SharePoint 2010中,我们可以使用Visual Studio 2010轻松创建WSP包来安装Web ...

  7. App.config提示错误“配置系统未能初始化”

    解决: "如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素." 所以它前面如果有 ...

  8. VC一些经验系列:《平均绘画矩形图,双击全屏》

    1.RGB宏报错 RGB宏是这样的, #define RGB(r,g,b)          ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))| ...

  9. CCClippingNode bug

    在Cocos2d-x v2.x,CCClippingNode类有一个bug,导致区域裁剪失效,由于Cocostudio的UIScrollview组件是基于该类构建的,所以这个问题是比较常见的,fix的 ...

  10. Weka-学习

    1.在java中使用Weka的eclipse配置方法 http://ianma.wordpress.com/2010/01/16/weka-with-java-eclipse-getting-star ...