1. package com.smartdot.dcu;
  2. /**
  3. * java获取新浪天气预报代码
  4. */
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.FileInputStream;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.net.URLEncoder;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import javax.xml.parsers.DocumentBuilder;
  16. import javax.xml.parsers.DocumentBuilderFactory;
  17. import javax.xml.parsers.ParserConfigurationException;
  18. import org.w3c.dom.Document;
  19. import org.w3c.dom.Element;
  20. import org.w3c.dom.Node;
  21. import org.w3c.dom.NodeList;
  22. import org.xml.sax.SAXException;
  23. /**
  24. * 解析xml文档,包括本地文档和url
  25. *
  26. */
  27. public class CommonsWeatherUtils {
  28. InputStream inStream;
  29. Element root;
  30. public InputStream getInStream() {
  31. return inStream;
  32. }
  33. public void setInStream(InputStream inStream) {
  34. this.inStream = inStream;
  35. }
  36. public Element getRoot() {
  37. return root;
  38. }
  39. public void setRoot(Element root) {
  40. this.root = root;
  41. }
  42. public CommonsWeatherUtils() {
  43. }
  44. /**
  45. * 通过输入流来获取新浪接口信息
  46. * @param inStream
  47. */
  48. public CommonsWeatherUtils(InputStream inStream) {
  49. if (inStream != null) {
  50. this.inStream = inStream;
  51. DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
  52. try {
  53. DocumentBuilder domBuilder = domfac.newDocumentBuilder();
  54. Document doc = domBuilder.parse(inStream);
  55. root = doc.getDocumentElement();
  56. } catch (ParserConfigurationException e) {
  57. e.printStackTrace();
  58. } catch (SAXException e) {
  59. e.printStackTrace();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. public CommonsWeatherUtils(String path) {
  66. InputStream inStream = null;
  67. try {
  68. inStream = new FileInputStream(path);
  69. } catch (FileNotFoundException e1) {
  70. e1.printStackTrace();
  71. }
  72. if (inStream != null) {
  73. this.inStream = inStream;
  74. DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
  75. try {
  76. DocumentBuilder domBuilder = domfac.newDocumentBuilder();
  77. Document doc = domBuilder.parse(inStream);
  78. root = doc.getDocumentElement();
  79. } catch (ParserConfigurationException e) {
  80. e.printStackTrace();
  81. } catch (SAXException e) {
  82. e.printStackTrace();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. public CommonsWeatherUtils(URL url) {
  89. InputStream inStream = null;
  90. try {
  91. inStream = url.openStream();
  92. } catch (IOException e1) {
  93. e1.printStackTrace();
  94. }
  95. if (inStream != null) {
  96. this.inStream = inStream;
  97. DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
  98. try {
  99. DocumentBuilder domBuilder = domfac.newDocumentBuilder();
  100. Document doc = domBuilder.parse(inStream);
  101. root = doc.getDocumentElement();
  102. } catch (ParserConfigurationException e) {
  103. e.printStackTrace();
  104. } catch (SAXException e) {
  105. e.printStackTrace();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111. /**
  112. *
  113. * @param nodes
  114. * @return 单个节点多个值以分号分隔
  115. */
  116. public Map<String, String> getValue(String[] nodes) {
  117. if (inStream == null || root==null) {
  118. return null;
  119. }
  120. Map<String, String> map = new HashMap<String, String>();
  121. // 初始化每个节点的值为null
  122. for (int i = 0; i < nodes.length; i++) {
  123. map.put(nodes[i], null);
  124. }
  125. // 遍历第一节点
  126. NodeList topNodes = root.getChildNodes();
  127. if (topNodes != null) {
  128. for (int i = 0; i < topNodes.getLength(); i++) {
  129. Node book = topNodes.item(i);
  130. if (book.getNodeType() == Node.ELEMENT_NODE) {
  131. for (int j = 0; j < nodes.length; j++) {
  132. for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) {
  133. if (node.getNodeType() == Node.ELEMENT_NODE) {
  134. if (node.getNodeName().equals(nodes[j])) {
  135. String val = node.getTextContent();
  136. String temp = map.get(nodes[j]);
  137. if (temp != null && !temp.equals("")) {
  138. temp = temp + ";" + val;
  139. } else {
  140. temp = val;
  141. }
  142. map.put(nodes[j], temp);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return map;
  151. }
  152. public String getWeather(String address){
  153. String weathers = "";
  154. String addressGBK = "";
  155. try {
  156. addressGBK = URLEncoder.encode(address, "GBK");
  157. } catch (UnsupportedEncodingException e2) {
  158. e2.printStackTrace();
  159. }
  160. String link = "http://php.weather.sina.com.cn/xml.php?city="+addressGBK+"&password=DJOYnieT8234jlsK&day=0";
  161. URL url;
  162. try {
  163. url = new URL(link);
  164. CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
  165. // 城市 白天天气,白天温度,夜晚天气,夜晚温度,白天风向,白天风力,夜晚风向,夜晚风力,污染指数,污染扩散指数
  166. String[] nodes = {"city","status1","temperature1","status2","temperature2","direction1","power1","direction2","power2","pollution_l","pollution_s"};
  167. Map<String, String> map = parser.getValue(nodes);
  168. System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+
  169. " 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+
  170. " 最低温度:"+map.get(nodes[4])+"℃ "+map.get(nodes[5])+map.get(nodes[6])+
  171. map.get(nodes[7])+map.get(nodes[8])+map.get(nodes[9])+map.get(nodes[10]));
  172. weathers = map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ";
  173. } catch (MalformedURLException e) {
  174. e.printStackTrace();
  175. }
  176. return weathers;
  177. }
  178. public static void main(String[] args) {
  179. CommonsWeatherUtils cwu = new CommonsWeatherUtils();
  180. System.out.println(cwu.getWeather("大连"));
  181. //String link="http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";
  182. //String link = "http://php.weather.sina.com.cn/search.php?city=%B4%F3%C1%AC&dpc=1";
  183. // String link = "http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT8234jlsK&day=0";
  184. // try {
  185. // System.out.println(URLEncoder.encode("北京", "GBK"));
  186. // } catch (UnsupportedEncodingException e1) {
  187. // // TODO Auto-generated catch block
  188. // e1.printStackTrace();
  189. // }
  190. //
  191. // URL url;
  192. //
  193. // try {
  194. //
  195. // url = new URL(link);
  196. // CommonsWeatherUtils parser = new CommonsWeatherUtils(url);
  197. // String[] nodes = {"city","status1","temperature1","status2","temperature2"};
  198. // Map<String, String> map = parser.getValue(nodes);
  199. // System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ");
  200. // } catch (MalformedURLException e) {
  201. // e.printStackTrace();
  202. // }
  203. }
  204. }

新浪天气api的更多相关文章

  1. linux - 使用curl实现新浪天气API应用

    新浪天气API的使用方法: API地址:http://php.weather.sina.com.cn/xml.php?city=%B1%B1%BE%A9&password=DJOYnieT82 ...

  2. 根据新浪天气API获取各地天气状况(Java实现)

    原文出自 参考网址(重要) http://blog.csdn.net/cyxlzzs/article/details/7602469  新浪 http://blog.csdn.net/l_ch_g/a ...

  3. 获取新浪天气api显示天气情况(转)

    直接上一个html的demo <!doctype html> <html class="no-js fixed-layout"> <head> ...

  4. android WebView将新浪天气为我所用 ------>仅供娱乐

    新浪天气提供了一个网页     http://w.sina.com 浏览器访问: 这效果还可以了哦,直接用webview加载出来,效果也可以了哦,不过,这不是我要的.我不希望在我写的应用里到处铺满si ...

  5. 新浪新闻API

    新浪新闻API ustcmio 关注 2017.01.15 20:44* 字数 536 阅读 2479评论 2喜欢 7 新浪新闻的API:1.访问手机新浪网https://sina.cn/?from= ...

  6. scrapy新浪天气

    一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用到桌面上的程序: LX终端(LXTermin ...

  7. [threeJs][新浪股票api][css3]3D新浪财经数据-最近A股涨的也太疯了......

    使用threeJS搭配新浪股票财经API 在线: http://wangxinsheng.herokuapp.com/stock 截图: A股涨幅榜[一片红10%] 检索[单击添加到自选内,自选使用l ...

  8. 新浪通过API分享 实践

    注:如果集成了百度的Frontia和SinaCoreSDK, 那么SSO会出现包冲突 https://github.com/sinaweibosdk/weibo_android_sdk/issues/ ...

  9. 新浪 股票 API

    新浪期货数据接口 [例子]http://hq.sinajs.cn/list=M0豆粕连续 M0 返回值如下:var hq_str_M0="豆粕连续,145958,3170,3190,3145 ...

随机推荐

  1. TheFatRat一般使用

    利用它生成后门 第一种通常情况下速度很快,较稳定,但免杀效果极差 推荐使用第二种 免杀效果好,如下图 第一种是最新的模块,较免杀效果极好.还可伪造图标 第二种是旧的

  2. poj1733(并查集+离散化)

    题目大意:有一个长度为n的0,1字符串, 给m条信息,每条信息表示第x到第y个字符中间1的个数为偶数个或奇数个, 若这些信息中第k+1是第一次与前面的话矛盾, 输出k; 思路:x, y之间1的个数为偶 ...

  3. 使用quartz数据库锁实现定时任务的分布式部署

    ,1.根据项目引用的quartz依赖版本,确定下载的quartz-distribution安装包,我项目引用的信息如下图所示: 2.解压,在\quartz-2.2.3-distribution\qua ...

  4. 单源最短路——Dijkstara算法

    算法基本思想:每次找到离源点最近的一个顶点,然后以该顶点为中心进行扩展,最终得到源点到其余所有点的最短路径. 1.将所有的顶点分为两个部分:已知最短路程的顶点集合P和未知最短路径的顶点集合Q 2.设置 ...

  5. 转:图解C#的值类型,引用类型,栈,堆,ref,out

    C# 的类型系统可分为两种类型,一是值类型,一是引用类型,这个每个C#程序员都了解.还有托管堆,栈,ref,out等等概念也是每个C#程序员都会接触到的概念,也是C#程序员面试经常考到的知识,随便搜搜 ...

  6. function(){}、var fun=function(){}和function fun(){}的区别

    一.基本定义 1.函数声明:使用function声明函数,并指定函数名. function fun() { // ...... } 2.函数表达式:使用function声明函数,但未指定函数名,将匿名 ...

  7. markdown的css样式(自己写的)

    markdown的css样式,这些是我自己配置的,感觉可以的话你可以添加下,不适合自己的话可以仿照第二种自己写个比较好的css样式. 第一种 /* RESET ==================== ...

  8. tp剩余未验证内容-7

    bash脚本中 的 set -e表示 exit immediately if a simple command returns a non-zero value.主要是为了防止错误被忽略.会被立即退出 ...

  9. [JSONObject/JSONArray] - 定制的JSON格式返回

    当前开发的程序中.因为抛弃了jsp的渲染,改为thymeleaf,并在比较厉害的前端进行数据json的渲染无误后,得出此json数据返回. 以往的Map<String,Object>返回j ...

  10. 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 ...