新浪天气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 ...
随机推荐
- TheFatRat一般使用
利用它生成后门 第一种通常情况下速度很快,较稳定,但免杀效果极差 推荐使用第二种 免杀效果好,如下图 第一种是最新的模块,较免杀效果极好.还可伪造图标 第二种是旧的
- poj1733(并查集+离散化)
题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...
- 使用quartz数据库锁实现定时任务的分布式部署
,1.根据项目引用的quartz依赖版本,确定下载的quartz-distribution安装包,我项目引用的信息如下图所示: 2.解压,在\quartz-2.2.3-distribution\qua ...
- 单源最短路——Dijkstara算法
算法基本思想:每次找到离源点最近的一个顶点,然后以该顶点为中心进行扩展,最终得到源点到其余所有点的最短路径. 1.将所有的顶点分为两个部分:已知最短路程的顶点集合P和未知最短路径的顶点集合Q 2.设置 ...
- 转:图解C#的值类型,引用类型,栈,堆,ref,out
C# 的类型系统可分为两种类型,一是值类型,一是引用类型,这个每个C#程序员都了解.还有托管堆,栈,ref,out等等概念也是每个C#程序员都会接触到的概念,也是C#程序员面试经常考到的知识,随便搜搜 ...
- function(){}、var fun=function(){}和function fun(){}的区别
一.基本定义 1.函数声明:使用function声明函数,并指定函数名. function fun() { // ...... } 2.函数表达式:使用function声明函数,但未指定函数名,将匿名 ...
- markdown的css样式(自己写的)
markdown的css样式,这些是我自己配置的,感觉可以的话你可以添加下,不适合自己的话可以仿照第二种自己写个比较好的css样式. 第一种 /* RESET ==================== ...
- tp剩余未验证内容-7
bash脚本中 的 set -e表示 exit immediately if a simple command returns a non-zero value.主要是为了防止错误被忽略.会被立即退出 ...
- [JSONObject/JSONArray] - 定制的JSON格式返回
当前开发的程序中.因为抛弃了jsp的渲染,改为thymeleaf,并在比较厉害的前端进行数据json的渲染无误后,得出此json数据返回. 以往的Map<String,Object>返回j ...
- Lintcode40-Implement Queue by Two Stacks-Medium
40. Implement Queue by Two Stacks As the title described, you should only use two stacks to implemen ...