根据新浪天气API获取各地天气状况(Java实现)
原文出自
参考网址(重要)
http://blog.csdn.net/cyxlzzs/article/details/7602469 新浪
http://blog.csdn.net/l_ch_g/article/details/8205817 新浪
http://blog.csdn.net/killtl/article/details/7312514 新浪
http://blog.csdn.net/qq910894904/article/details/7540093 新浪
http://blog.sina.com.cn/s/blog_417845750101d5ws.html 国家气象局
http://www.verydemo.com/demo_c131_i42456.html 国家气象局
http://blog.csdn.net/hello_haozi/article/details/7564223 国家气象局
http://www.oschina.net/code/snippet_96894_17983 中国天气网api
1、很多时候我们会需要在自己的应用上面显示天气状况,这种情况我们只能借助第三方的API来进行实现
2、这里我们讲一下如何获取新浪API提供的天气
1)首先我们在浏览器中访问地址“http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0”。这时我们看到的是一个关于重庆的天气状况的一个xml文档。仔细观察该地址,我们发现如果我们要查看其它城市的天气时只要将city后面的参数换成你想要的城市,也许你会认为city的值怎么是一推看不懂的字符,如果你在百度一下框中输入重庆两个字后点击按钮后你会发现url变成了“http://www.baidu.com/s?wd=%D6%D8%C7%EC&rsv_bp=0&rsv_spt=3&inputT=2574”,比对一下wd参数值就可以知道,它就是重庆两个字的另一种编码方式
2)好了,现在我们得到了某个城市天气状况的xml文档,我们想要得到我们的天气描述主要解析该文档就好了,接下来我们就编码实现java解析xml文档
3)代码如下
- package com.quickmanager.util;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.FileInputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- /**
- * 解析xml文档,包括本地文档和url
- * @author cyxl
- * @version 1.0 2012-05-24
- * @since 1.0
- *
- */
- public class XmlParser {
- InputStream inStream;
- Element root;
- public InputStream getInStream() {
- return inStream;
- }
- public void setInStream(InputStream inStream) {
- this.inStream = inStream;
- }
- public Element getRoot() {
- return root;
- }
- public void setRoot(Element root) {
- this.root = root;
- }
- public XmlParser() {
- }
- public XmlParser(InputStream inStream) {
- if (inStream != null) {
- this.inStream = inStream;
- DocumentBuilderFactory domfac = DocumentBuilderFactory
- .newInstance();
- try {
- DocumentBuilder domBuilder = domfac.newDocumentBuilder();
- Document doc = domBuilder.parse(inStream);
- root = doc.getDocumentElement();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public XmlParser(String path) {
- InputStream inStream = null;
- try {
- inStream = new FileInputStream(path);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- if (inStream != null) {
- this.inStream = inStream;
- DocumentBuilderFactory domfac = DocumentBuilderFactory
- .newInstance();
- try {
- DocumentBuilder domBuilder = domfac.newDocumentBuilder();
- Document doc = domBuilder.parse(inStream);
- root = doc.getDocumentElement();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public XmlParser(URL url) {
- InputStream inStream = null;
- try {
- inStream = url.openStream();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- if (inStream != null) {
- this.inStream = inStream;
- DocumentBuilderFactory domfac = DocumentBuilderFactory
- .newInstance();
- try {
- DocumentBuilder domBuilder = domfac.newDocumentBuilder();
- Document doc = domBuilder.parse(inStream);
- root = doc.getDocumentElement();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- *
- * @param nodes
- * @return 单个节点多个值以分号分隔
- */
- public Map<String, String> getValue(String[] nodes) {
- if (inStream == null || root==null) {
- return null;
- }
- Map<String, String> map = new HashMap<String, String>();
- // 初始化每个节点的值为null
- for (int i = 0; i < nodes.length; i++) {
- map.put(nodes[i], null);
- }
- // 遍历第一节点
- NodeList topNodes = root.getChildNodes();
- if (topNodes != null) {
- for (int i = 0; i < topNodes.getLength(); i++) {
- Node book = topNodes.item(i);
- if (book.getNodeType() == Node.ELEMENT_NODE) {
- for (int j = 0; j < nodes.length; j++) {
- for (Node node = book.getFirstChild(); node != null; node = node
- .getNextSibling()) {
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- if (node.getNodeName().equals(nodes[j])) {
- //String val=node.getFirstChild().getNodeValue();
- String val = node.getTextContent();
- System.out.println(nodes[j] + ":" + val);
- // 如果原来已经有值则以分号分隔
- String temp = map.get(nodes[j]);
- if (temp != null && !temp.equals("")) {
- temp = temp + ";" + val;
- } else {
- temp = val;
- }
- map.put(nodes[j], temp);
- }
- }
- }
- }
- }
- }
- }
- return map;
- }
- }
4)测试代码如下
- public static void main(String[] args) {
- String link = "http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";
- URL url;
- String path = "test.xml";
- try {
- url = new URL(link);
- System.out.println(url);
- // InputStream inStream= url.openStream();
- // InputStream inStream=new FileInputStream(new File("test.xml"));
- XmlParser parser = new XmlParser(url);
- String[] nodes = {"status1","temperature1","temperature2"};
- Map<String, String> map = parser.getValue(nodes);
- System.out.println(map.get(nodes[0]));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
5)输出结果
- http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0
- status1:阵雨
- temperature1:21
- temperature2:18
- 阵雨
6)说明。改类的主要方法为getValue,传入的参数一个节点名字数组。具体可以参考测试代码,测试代码中我们获取了天气、最低温度和最高温度三项。构造方法重载了三种方式,第一种为直接传入字符流,第二种为传入本地xml文档的路径,第三种为传入一个URL对象,我们获取天气时就是采用了第三种方式,因为我们是从互联网上获取的一个页面数据
附加:最近在浏览CSDN时发现另外有篇文章获取天气信息的。感觉还是挺方便的,在这里分享一下:http://blog.csdn.net/hello_haozi/article/details/7564223
根据新浪天气API获取各地天气状况(Java实现)的更多相关文章
- 使用新浪IP库获取IP详细地址
使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...
- 新浪新闻API
新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...
- 使用小米天气API获取天气信息
1. URL部分 以下url中"%s"代表的是城市Id,比如北京的cityId=101010100: //获取未来五天预报信息,红色部分信息不需要 WEATHER_DATA_URL ...
- 新浪通过API分享 实践
注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突 https://github.com/sinaweibosdk/weibo_android_sdk/issues/ ...
- [threeJs][新浪股票api][css3]3D新浪财经数据-最近A股涨的也太疯了......
使用threeJS搭配新浪股票财经API 在线: http://wangxinsheng.herokuapp.com/stock 截图: A股涨幅榜[一片红10%] 检索[单击添加到自选内,自选使用l ...
- curl实例-通过新浪股票接口获取股票信息
在学习curl的过程中,我们知道curl是相当于一个简单的浏览器,通过往对应的服务上面发送数据信息,返回服务器的响应结果,这个在Java里面主要是使用封装好的httpclient来进行操作,但是自己认 ...
- 新浪 股票 API
新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...
- 新浪新闻API接口
头条 http://api.sina.cn/sinago/list.json?channel=news_toutiao推荐 http://api.sina.cn/sinago/list.json?ch ...
- scrapy新浪天气
一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面上的程序: LX终端(LXTermin ...
随机推荐
- elastic search 学习笔记
Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取 ...
- POJ 1236.Network of Schools (强连通)
首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...
- 不学就吃亏的underscorejs类库学习示例 ——(集合篇)
underscorejs是一个很不错的类库,我的很多项目都引用了这个类库,的确可以带来很多方便. 记得我当初学的时候,看underscorejs的api是看的一知半解的,甚至不明白api里的conte ...
- ECSHOP在商品详细页面上获取该商品的顶级分类id和名称
在 goods.php 文件, 找到 $smarty->assign('goods', $goods); 在它上面增加下面代码: 方法一: $cat_arr = get_parent_cats( ...
- JS 获取各个宽度和高度
IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...
- JavaScript解析机制
JavaScript是一种解释型语言,按照<script>块儿来预编译和执行. JavaScript解释器在预编译阶段,先预声明变量,再预声明函数.在执行阶段,进行变量赋值,和函数执行. ...
- Python Tutorial 学习(四)--More Control Flow Tools
4.1 if 表达式 作为最为人熟知的if.你肯定对这样的一些表达式不感到陌生: >>> x = int(raw_input("Please enter an intege ...
- google chrome中如何删除一条输入网址提示
在google chrome中网站栏输入字母的时候会出现网址的提示,如下图: 之前遇到个问题,不知道之前打错了www.baidu.com为wwww.baidu.com(也会跳转到百度)导致一输入“w” ...
- coroutine协程
如果你接触过lua这种小巧的脚本语言,你就会经常接触到一个叫做协程的神奇概念.大多数脚本语言都有对协程不同程度的支持.但是大多编译语言,如C/C++,根本就不知道这样的东西存在.当然也很多人研究如何在 ...
- [BZOJ 1576] [Usaco2009 Jan] 安全路经Travel 【树链剖分】
题目链接: BZOJ - 1576 题目分析 首先Orz Hzwer的题解. 先使用 dijikstra 求出最短路径树. 那么对于一条不在最短路径树上的边 (u -> v, w) 我们可以先沿 ...