新浪天气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 ...
随机推荐
- Streaming从Spark2X迁移到Spark1.5 summary
配置文件的加载是一个难点,在local模式下非常容易,但是submit后一直报找不到文件,后来采用将properties文件放在加载类同一个package下,打包到同一个jar中解决. import ...
- pypi上传命令
windows 1.新建一个setup.py文件与你自己写的.py模块放在一个文件夹内 内容: from distutils.core import setup setup( name = " ...
- 移动端长按响应事件以及阻止默认行为e.preventDefault()导致定时器setTimeout不能响应
手指触摸绑定: $(document).on('touchstart', '.photo', function(e){ currentIndex = parseInt($(this).index('. ...
- Linux vim文档操作
1.vim passwd文件 出现 发现文件passwd.swp文件,无法正常编辑vim的时候,删除掉.psswd.swp即可 vim下:dd #删除一行 vim下:p #粘贴一行 vim下:y #复 ...
- 剑指offer(54)字符流中第一个不重复的数字
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- nginx访问不到
nginx访问不到 今天,一朋友的一台linux服务器上部署了nginx,但是外部(公网)就是不能访问,于是协助其排查.整体思路如下: 1.确认nginx配置是否ok. 2.确认网络是否可达. 3.是 ...
- freeswitch 显示主叫名称和主叫号码
1.指定主叫号码 origination_caller_id_number 参数来指定显示的主叫号码 2.指定主叫名称 origination_caller_id_name 参数来指定显示的主叫名称 ...
- eclipse Oxygen2 4.7.2版本安装activiti插件,并兼容svn插件
附录,插件下载:链接:https://pan.baidu.com/s/138ChoXao1fALBzdOhJjdQg 密码:06fx 安装方法: 解压eclipse安装包,将eclipse-activ ...
- Static需谨慎
Static Cling Sticking Your Code To Things Unnecessarily Static Cling is a code smell used to describ ...
- Linux文件结构
/: 根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. /bin: bin 就是二进制(binary)英文缩写.在一般的系统当中,都可以在这个目录下找到 ...