新浪天气api
package com.smartdot.dcu;
/**
* java获取新浪天气预报代码
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
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
*
*/
public class CommonsWeatherUtils {
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 CommonsWeatherUtils() {
}
/**
* 通过输入流来获取新浪接口信息
* @param inStream
*/
public CommonsWeatherUtils(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 CommonsWeatherUtils(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 CommonsWeatherUtils(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.getTextContent();
String temp = map.get(nodes[j]);
if (temp != null && !temp.equals("")) {
temp = temp + ";" + val;
} else {
temp = val;
}
map.put(nodes[j], temp);
}
}
}
}
}
}
}
return map;
}
public String getWeather(String address){
String weathers = "";
String addressGBK = "";
try {
addressGBK = URLEncoder.encode(address, "GBK");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
String link = "http://php.weather.sina.com.cn/xml.php?city="+addressGBK+"&password=DJOYnieT8234jlsK&day=0";
URL url;
try {
url = new URL(link);
CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
// 城市 白天天气,白天温度,夜晚天气,夜晚温度,白天风向,白天风力,夜晚风向,夜晚风力,污染指数,污染扩散指数
String[] nodes = {"city","status1","temperature1","status2","temperature2","direction1","power1","direction2","power2","pollution_l","pollution_s"};
Map<String, String> map = parser.getValue(nodes);
System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+
" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+
" 最低温度:"+map.get(nodes[4])+"℃ "+map.get(nodes[5])+map.get(nodes[6])+
map.get(nodes[7])+map.get(nodes[8])+map.get(nodes[9])+map.get(nodes[10]));
weathers = map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ";
} catch (MalformedURLException e) {
e.printStackTrace();
}
return weathers;
}
public static void main(String[] args) {
CommonsWeatherUtils cwu = new CommonsWeatherUtils();
System.out.println(cwu.getWeather("大连"));
//String link="http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";
//String link = "http://php.weather.sina.com.cn/search.php?city=%B4%F3%C1%AC&dpc=1";
// String link = "http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0";
// try {
// System.out.println(URLEncoder.encode("北京", "GBK"));
// } catch (UnsupportedEncodingException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
//
// URL url;
//
// try {
//
// url = new URL(link);
// CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
// String[] nodes = {"city","status1","temperature1","status2","temperature2"};
// Map<String, String> map = parser.getValue(nodes);
// System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ");
// } catch (MalformedURLException e) {
// e.printStackTrace();
// }
}
}
新浪天气api的更多相关文章
- linux - 使用curl实现新浪天气API应用
新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...
- 根据新浪天气API获取各地天气状况(Java实现)
原文出自 参考网址(重要) http://blog.csdn.net/cyxlzzs/article/details/7602469 新浪 http://blog.csdn.net/l_ch_g/a ...
- 获取新浪天气api显示天气情况(转)
直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...
- android WebView将新浪天气为我所用 ------>仅供娱乐
新浪天气提供了一个网页 http://w.sina.com 浏览器访问: 这效果还可以了哦,直接用webview加载出来,效果也可以了哦,不过,这不是我要的.我不希望在我写的应用里到处铺满si ...
- 新浪新闻API
新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...
- scrapy新浪天气
一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面上的程序: LX终端(LXTermin ...
- [threeJs][新浪股票api][css3]3D新浪财经数据-最近A股涨的也太疯了......
使用threeJS搭配新浪股票财经API 在线: http://wangxinsheng.herokuapp.com/stock 截图: A股涨幅榜[一片红10%] 检索[单击添加到自选内,自选使用l ...
- 新浪通过API分享 实践
注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突 https://github.com/sinaweibosdk/weibo_android_sdk/issues/ ...
- 新浪 股票 API
新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...
随机推荐
- HDU 2586 How far away(dfs+邻接表)
How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...
- C#对象序列化成XML,以及自定义标签名
C#对象序列化操作: public class XMLHelper { /// <summary> /// 对象序列化成 XML String /// </summary> p ...
- LeetCode258 各位相加
题目链接:https://leetcode-cn.com/problems/add-digits/ 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: ...
- C#简单的九九乘法表
for(int i=1;i<10;i++) { for(int j=1;j<=i;j++) { Console.Write("{0}*{1}={2}",j,i,i*j) ...
- QQ"坦白说"抓包破解与PacketCapture使用介绍
据腾讯发布内容来看,“坦白说”是刚刚在QQ中上线的新功能,还在测试阶段就已经非常火爆. 但作为一种web端的小游戏,无疑可以使用爬虫的来自我模拟. (话说写完这篇的时候我总感觉自己几年前好像写过这个. ...
- 【Alpha】Scrum Meeting 2
前言 第2次会议在3月28日由PM在教一317召开. 主要确定了项目方向和目标功能,进行了任务分工.时长60min. 任务分配 姓名 当前阶段任务 下阶段任务 吴昊 熟悉代码和配置环境,发布手机端博客 ...
- 7.24-Codeforces Round #494 (Div. 3)
链接:http://codeforces.com/contest/1003 A. Polycarp's Pockets 题型:模拟 题意:把初始集合拆分,要求相同的数不在同一个集合中,求出需要的集合个 ...
- Qt Windows打开指定文件注意替换双斜杠为单斜杠
QProcess::startDetached(QString("explorer %1").arg(strFilePath)); 其中,在windows上使用时,strFileP ...
- vue+vux scrollTop无法实现定位到具体dom
先看一下最终的运行效果. 项目背景介绍:技术栈: vue+vux+nodejs需求:对汽车品牌列表可以按照字母进行索引定位 在开发中实现这种需求,心想还不是小菜一碟,作为一个饱经bug的程序员,别的我 ...
- [POI1999][LOJ10112]原始生物
典型的有向图K笔画的问题 最后答案就是n+1-1+k 1笔画有一点入度比出度少1 k笔画则统计入度比出度少的点中所有少的总和 #include<bits/stdc++.h> using n ...