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();
}
}
}
Java调用yahoo!API获取天气数据的更多相关文章
- 高德地图API获取天气
1.建立行政区规划清单表 use edw; drop table if exists dim_prov_city_adcode; create table if not exists dim_prov ...
- 调用ZoomEye API获取信息
最近在提高自己编程能力,拿一些实用的小工具练下.该脚本为python语言,主要涉及模块urllib,json,os模块. 功能:调用ZoomEye API获取信息 import urllib.requ ...
- Java通过webservice接口获取天气信息
通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...
- Android访问中央气象台的天气预报API得到天气数据
最新说明:该接口已失效! 2014-03-04 可申请它公布的API,需申请:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml 在用A ...
- Java 调用 Hbase API 访问接口实现方案
HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件 ...
- Java 调用Restful API接口的几种方式--HTTPS
摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...
- Java 调用 Rest api 设置经典 Linux 虚拟机的实例启停
现象描述 用户可以通过 Rest API 设置经典 Linux 虚拟机实例的启停.在调用该 API 时需要通过 Azure Active Directory(下文简称 AAD) 获取 Token,但是 ...
- java解析xml实例——获取天气信息
获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...
- 使用Python调用Flickr API抓取图片数据
Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...
随机推荐
- Fast UI Draw (Intel出品)
Fast UI Draw in a library that provides a higher performance Canvas interface. It is designed so tha ...
- webstore+nodejs
新建一个普通的project. 编写如下代码: var http=require('http'); http.createServer(function(req,res){ res.writeHead ...
- absolute和relative的几个Demo
这些例子最好通过FireFox结合FireBug调试查看 1.absolute让元素inline-block化 <!DOCTYPE html> <html xmlns="h ...
- poj3280Cheapest Palindrome(记忆化)
链接 真的1A了.. 一开始想复杂了 想着补全再删 没想好 后来想到递归 大的回文串是由小的推过来的 一直递归下去 对于当前的i,j可以选择保留或者删除 选个最小的 #include <iost ...
- hdu4649Professor Tian
http://acm.hdu.edu.cn/showproblem.php?pid=4649 枚举当前状态是0或者是1的概率 其实就枚举1的概率 最后概率乘这个数 位运算用得不熟 老出错 #incl ...
- 获取Mac、CPUID、硬盘序列号、本地IP地址、外网IP地址OCX控件
提供获取Mac.CPUID.硬盘序列号.本地IP地址.外网IP地址OCX控件 开发语言:vc++ 可应用与WEB程序开发应用 <HTML><HEAD><TITLE> ...
- SharePoint Services 数据库表
转:http://dugan.bokee.com/630446.html 以下是Microsoft Windows SharePoint Services架构中数据库列表. Configuration ...
- POJ 3254 (状态压缩DP)
思路:状态压缩dp,用二进制位的1表示放了,0表示没有放.设dp[i][j],表示第i行状态为j时,前i行的方案数,状态转移方程就是 dp[i][j] += dp[i-1][k],j与k这两个状态不冲 ...
- [BILL WEI]SQL 如何将查询到的列作为表名去查询数据
我们在做sql查询的时候,有时候需要将查询的列作为表名,去引用,然后再次查询 declare @table_name varchar(20) select @table_name=table_name ...
- Unity3d Realtime Dynamic Volume Clouds Rendering
Ray Marching体积渲染+perlin noise 动态效果: 博主近期渲染: 2016的渲染 2015后半段的渲染 ---- by wolf96