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

 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();
}
}
}

最新文章

  1. [css3]跑马灯
  2. css实现省略号
  3. js使用转义符技巧输出HTML
  4. python 自定义排序函数
  5. CSS笔记(十一)CSS3之边框
  6. redirect模块的秘密
  7. 对Web标准的理解
  8. 计算视频播放的时间(pts)
  9. C# Tcp协议收发数据
  10. 协议(porotocol)
  11. UVA 11802 All Your Bases Belong to Us
  12. 8个开发必备的PHP功能(转)
  13. 关于web标准的理解(转)
  14. Activity 的生命周期
  15. bzoj 3505 [Cqoi2014]数三角形 组合
  16. 【Centos】Postgresql连接测试(Perl和Ruby)
  17. 封装自己的dapper lambda扩展-设计篇
  18. position:fixed固定定位的用法
  19. 题解-bzoj2154Crash的数字表格 &amp; bzoj2693 jzptab
  20. 【转载】MapReduce编程 Intellij Idea配置MapReduce编程环境

热门文章

  1. Spring事务管理--多个ORM框架在使用时的情况分析
  2. leetcode面试准备:Reverse Words in a String
  3. Oracle系列之函数
  4. 基于SXSSF (Streaming Usermodel API)的写文件
  5. BZOJ_1010_[HNOI2008]_玩具装箱toy_(斜率优化动态规划+单调队列)
  6. ☀【移动】UC极速模式
  7. 宣布 Azure Backup 支持备份 Windows Server 2008
  8. GIS数据格式:Shapefile
  9. POJ --- 2918 求解数独
  10. Unity给力插件之ShaderForge(三)