zoukankan      html  css  js  c++  java
  • 根据新浪天气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)代码如下

     

    [java]  view plain copy
     
    1. package com.quickmanager.util;  
    2.   
    3. import java.io.FileNotFoundException;  
    4. import java.io.IOException;  
    5. import java.io.InputStream;  
    6. import java.io.FileInputStream;  
    7. import java.net.MalformedURLException;  
    8. import java.net.URL;  
    9. import java.util.HashMap;  
    10. import java.util.Map;  
    11.   
    12. import javax.xml.parsers.DocumentBuilder;  
    13. import javax.xml.parsers.DocumentBuilderFactory;  
    14. import javax.xml.parsers.ParserConfigurationException;  
    15.   
    16. import org.w3c.dom.Document;  
    17. import org.w3c.dom.Element;  
    18. import org.w3c.dom.Node;  
    19. import org.w3c.dom.NodeList;  
    20. import org.xml.sax.SAXException;  
    21.   
    22. /** 
    23.  * 解析xml文档,包括本地文档和url 
    24.  * @author cyxl 
    25.  * @version 1.0 2012-05-24 
    26.  * @since 1.0 
    27.  * 
    28.  */  
    29. public class XmlParser {  
    30.     InputStream inStream;  
    31.     Element root;  
    32.   
    33.     public InputStream getInStream() {  
    34.         return inStream;  
    35.     }  
    36.   
    37.     public void setInStream(InputStream inStream) {  
    38.         this.inStream = inStream;  
    39.     }  
    40.   
    41.     public Element getRoot() {  
    42.         return root;  
    43.     }  
    44.   
    45.     public void setRoot(Element root) {  
    46.         this.root = root;  
    47.     }  
    48.   
    49.     public XmlParser() {  
    50.     }  
    51.   
    52.     public XmlParser(InputStream inStream) {  
    53.         if (inStream != null) {  
    54.             this.inStream = inStream;  
    55.             DocumentBuilderFactory domfac = DocumentBuilderFactory  
    56.                     .newInstance();  
    57.             try {  
    58.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
    59.                 Document doc = domBuilder.parse(inStream);  
    60.                 root = doc.getDocumentElement();  
    61.             } catch (ParserConfigurationException e) {  
    62.                 e.printStackTrace();  
    63.             } catch (SAXException e) {  
    64.                 e.printStackTrace();  
    65.             } catch (IOException e) {  
    66.                 e.printStackTrace();  
    67.             }  
    68.         }  
    69.     }  
    70.   
    71.     public XmlParser(String path) {  
    72.         InputStream inStream = null;  
    73.         try {  
    74.             inStream = new FileInputStream(path);  
    75.         } catch (FileNotFoundException e1) {  
    76.             e1.printStackTrace();  
    77.         }  
    78.         if (inStream != null) {  
    79.             this.inStream = inStream;  
    80.             DocumentBuilderFactory domfac = DocumentBuilderFactory  
    81.                     .newInstance();  
    82.             try {  
    83.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
    84.                 Document doc = domBuilder.parse(inStream);  
    85.                 root = doc.getDocumentElement();  
    86.             } catch (ParserConfigurationException e) {  
    87.                 e.printStackTrace();  
    88.             } catch (SAXException e) {  
    89.                 e.printStackTrace();  
    90.             } catch (IOException e) {  
    91.                 e.printStackTrace();  
    92.             }  
    93.         }  
    94.     }  
    95.   
    96.     public XmlParser(URL url) {  
    97.         InputStream inStream = null;  
    98.         try {  
    99.             inStream = url.openStream();  
    100.         } catch (IOException e1) {  
    101.             e1.printStackTrace();  
    102.         }  
    103.         if (inStream != null) {  
    104.             this.inStream = inStream;  
    105.             DocumentBuilderFactory domfac = DocumentBuilderFactory  
    106.                     .newInstance();  
    107.             try {  
    108.                 DocumentBuilder domBuilder = domfac.newDocumentBuilder();  
    109.                 Document doc = domBuilder.parse(inStream);  
    110.                 root = doc.getDocumentElement();  
    111.             } catch (ParserConfigurationException e) {  
    112.                 e.printStackTrace();  
    113.             } catch (SAXException e) {  
    114.                 e.printStackTrace();  
    115.             } catch (IOException e) {  
    116.                 e.printStackTrace();  
    117.             }  
    118.         }  
    119.     }  
    120.   
    121.     /** 
    122.      *  
    123.      * @param nodes 
    124.      * @return 单个节点多个值以分号分隔 
    125.      */  
    126.     public Map<String, String> getValue(String[] nodes) {  
    127.         if (inStream == null || root==null) {  
    128.             return null;  
    129.         }  
    130.         Map<String, String> map = new HashMap<String, String>();  
    131.         // 初始化每个节点的值为null  
    132.         for (int i = 0; i < nodes.length; i++) {  
    133.             map.put(nodes[i], null);  
    134.         }  
    135.   
    136.         // 遍历第一节点  
    137.         NodeList topNodes = root.getChildNodes();  
    138.         if (topNodes != null) {  
    139.             for (int i = 0; i < topNodes.getLength(); i++) {  
    140.                 Node book = topNodes.item(i);  
    141.                 if (book.getNodeType() == Node.ELEMENT_NODE) {  
    142.                     for (int j = 0; j < nodes.length; j++) {  
    143.                         for (Node node = book.getFirstChild(); node != null; node = node  
    144.                                 .getNextSibling()) {  
    145.                             if (node.getNodeType() == Node.ELEMENT_NODE) {  
    146.                                 if (node.getNodeName().equals(nodes[j])) {  
    147.                                     //String val=node.getFirstChild().getNodeValue();  
    148.                                     String val = node.getTextContent();  
    149.                                     System.out.println(nodes[j] + ":" + val);  
    150.                                     // 如果原来已经有值则以分号分隔  
    151.                                     String temp = map.get(nodes[j]);  
    152.                                     if (temp != null && !temp.equals("")) {  
    153.                                         temp = temp + ";" + val;  
    154.                                     } else {  
    155.                                         temp = val;  
    156.                                     }  
    157.                                     map.put(nodes[j], temp);  
    158.                                 }  
    159.                             }  
    160.                         }  
    161.                     }  
    162.                 }  
    163.             }  
    164.         }  
    165.         return map;  
    166.     }  
    167.   
    168. }  


    4)测试代码如下

     

     

     

    [java]  view plain copy
     
    1. public static void main(String[] args) {  
    2.         String link = "http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";  
    3.         URL url;  
    4.         String path = "test.xml";  
    5.         try {  
    6.             url = new URL(link);  
    7.             System.out.println(url);  
    8.             // InputStream inStream= url.openStream();  
    9.             // InputStream inStream=new FileInputStream(new File("test.xml"));  
    10.             XmlParser parser = new XmlParser(url);  
    11.             String[] nodes = {"status1","temperature1","temperature2"};  
    12.             Map<String, String> map = parser.getValue(nodes);  
    13.             System.out.println(map.get(nodes[0]));  
    14.         } catch (MalformedURLException e) {  
    15.             e.printStackTrace();  
    16.         }  
    17.   
    18.     }  


    5)输出结果

     

     

     

    [plain]  view plain copy
     
    1. http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0  
    2. status1:阵雨  
    3. temperature1:21  
    4. temperature2:18  
    5. 阵雨  


    6)说明。改类的主要方法为getValue,传入的参数一个节点名字数组。具体可以参考测试代码,测试代码中我们获取了天气、最低温度和最高温度三项。构造方法重载了三种方式,第一种为直接传入字符流,第二种为传入本地xml文档的路径,第三种为传入一个URL对象,我们获取天气时就是采用了第三种方式,因为我们是从互联网上获取的一个页面数据

     

     

    附加:最近在浏览CSDN时发现另外有篇文章获取天气信息的。感觉还是挺方便的,在这里分享一下:http://blog.csdn.net/hello_haozi/article/details/7564223

  • 相关阅读:
    java去除string类型里面的中括号
    java深拷贝浅拷贝
    前端接受数据去除[" "]的方法
    sql 查询近6 /xx个月/天 统计数据
    vue 定时器(定时任务)
    vue 二维码判断手机登录是哪个操作系统
    Connection is read-only. Queries leading to data modification are not allowed
    redis一闪而退解决办法
    前端的模糊查找
    vue笔记生命周期里面的某些函数
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3458983.html
Copyright © 2011-2022 走看看