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

 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. .NET事件的指导原则

    C#允许编写所需的各种类型的事件.但是,为了与.NET Framwork的组件相兼容,开发人员必须遵循微软为此建立的一系列指导原则.这些指导原则的核心是,事件处理程序必须拥有两个参数.第一个参数是一个 ...

  2. Universal Asynchronous Receiver/Transmitter

    USART簡介與特性 NRZ標準資料格式(Mark/Space) 半雙工/全雙工 Synchronous 同步傳輸 CLOCK SKEW Asynchronous 非同步傳輸 半/全雙工.同步/非同步 ...

  3. MS提供的虚拟机IE测试

    https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

  4. ActionBar官方教程(10)ActionBar的下拉列表模式

    Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...

  5. 【HDOJ】3337 Guess the number

    神一样的题目.简言之,利用手段获得测试用例的第一行,输出结果.很显然利用wa, TLE, OLE等judge status可以获得测试用例.因此,果断Python写一个acm提交机器人.依赖lxml库 ...

  6. Understanding Item Import and Debugging Problems with Item Import (Doc ID 268968.1)

    In this Document Purpose Details   Scenario 1: Testing the basic item import with minimum columns po ...

  7. ☀【插件】iScroll

    iScroll 4.2.5 中文API √http://www.gafish.net/api/iScroll.html -webkit-overflow-scrolling: touch <!D ...

  8. ☀【组件】数组 array

    <!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  9. 使用 Azure Site Recovery 将内部部署虚拟化工作负荷迁移至 Azure

    ABHISHEK A. HEMRAJANI 云 + Enterprise 项目经理 我们最近发布了微软的灾难恢复即服务 (DRaaS) 产品的预览版 - Azure SiteRecovery.此产 ...

  10. GIS数据格式:Coverage

    转自:http://www.cnblogs.com/w2william/archive/2009/10/15/1583954.html 我之前做了一个模块,基本实现了ArcCatalog中左边树状结构 ...