天气API

最近项目中需要在首页添加一个天气的栏目,因此需要去获取天气的参数。

需要日期、天气状态、空气状态、pm2.5指数,温度、风向风力。

网上找了很多的接口很多都是没有pm2.5这个指数。偶然发现了一个带pm指数的接口。拿来试了一下,还不错,在此跟大家分享一下。

通过城市名(北京/北京市)获取当前城市温度、湿度、空气质量主要参数指数、警告、昨日天气、未来天气、日出日落时间((⊙﹏⊙)b)(XML数据):

接口:http://wthrcdn.etouch.cn/WeatherApi?city=北京市;

注意:1、此接口返回的XML数据,对于撸惯Json数据的小伙伴来说,需要再撸下XML解析了;2、台湾城市及大陆一些城市的空气质量数据没有;

用postman测试一下:

可以看到返回的是xml格式的数据。

我们要拿到其中有用的信息,这就需要对xml进行操作。我用的是dom4j。

在idea中新建一个类进行测:因为数据量比较大,所以我截取了其中一部分。

public class WeatherTest {
    public static void main(String[] args) {
        String resultStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "\n" +
                "<resp>\n" +
                "  <city>北京</city>\n" +
                "  <updatetime>21:26</updatetime>\n" +
                "  <wendu>16</wendu>\n" +
                "  <fengli><![CDATA[5级]]></fengli>\n" +
                "  <shidu>18%</shidu>\n" +
                "  <fengxiang>西北风</fengxiang>\n" +
                "  <sunrise_1>06:37</sunrise_1>\n" +
                "  <sunset_1>17:19</sunset_1>\n" +
                "  <sunrise_2/>\n" +
                "  <sunset_2/>\n" +
                "  <environment>\n" +
                "    <aqi>43</aqi>\n" +
                "    <pm25>13</pm25>\n" +
                "    <suggest>各类人群可自由活动</suggest>\n" +
                "    <quality>优</quality>\n" +
                "    <MajorPollutants/>\n" +
                "    <o3>45</o3>\n" +
                "    <co>0</co>\n" +
                "    <pm10>42</pm10>\n" +
                "    <so2>2</so2>\n" +
                "    <no2>32</no2>\n" +
                "    <time>21:00:00</time>\n" +
                "  </environment>\n" +
                "  <yesterday>\n" +
                "    <date_1>26日星期五</date_1>\n" +
                "    <high_1>高温 14℃</high_1>\n" +
                "    <low_1>低温 3℃</low_1>\n" +
                "    <day_1>\n" +
                "      <type_1>晴</type_1>\n" +
                "      <fx_1>西北风</fx_1>\n" +
                "      <fl_1><![CDATA[4-5级]]></fl_1>\n" +
                "    </day_1>\n" +
                "    <night_1>\n" +
                "      <type_1>晴</type_1>\n" +
                "      <fx_1>西北风</fx_1>\n" +
                "      <fl_1><![CDATA[<3级]]></fl_1>\n" +
                "    </night_1>\n" +
                "  </yesterday>\n" +
                "  <forecast>\n" +
                "    <weather>\n" +
                "      <date>27日星期六</date>\n" +
                "      <high>高温 17℃</high>\n" +
                "      <low>低温 5℃</low>\n" +
                "      <day>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>西南风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </day>\n" +
                "      <night>\n" +
                "        <type>多云</type>\n" +
                "        <fengxiang>西南风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </night>\n" +
                "    </weather>\n" +
                "    <weather>\n" +
                "      <date>28日星期天</date>\n" +
                "      <high>高温 17℃</high>\n" +
                "      <low>低温 4℃</low>\n" +
                "      <day>\n" +
                "        <type>多云</type>\n" +
                "        <fengxiang>西北风</fengxiang>\n" +
                "        <fengli><![CDATA[4-5级]]></fengli>\n" +
                "      </day>\n" +
                "      <night>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>无持续风向</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </night>\n" +
                "    </weather>\n" +
                "    <weather>\n" +
                "      <date>29日星期一</date>\n" +
                "      <high>高温 15℃</high>\n" +
                "      <low>低温 3℃</low>\n" +
                "      <day>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>无持续风向</fengxiang>\n" +
                "        <fengli><![CDATA[3-4级]]></fengli>\n" +
                "      </day>\n" +
                "      <night>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>北风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </night>\n" +
                "    </weather>\n" +
                "    <weather>\n" +
                "      <date>30日星期二</date>\n" +
                "      <high>高温 15℃</high>\n" +
                "      <low>低温 1℃</low>\n" +
                "      <day>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>北风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </day>\n" +
                "      <night>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>北风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </night>\n" +
                "    </weather>\n" +
                "    <weather>\n" +
                "      <date>31日星期三</date>\n" +
                "      <high>高温 15℃</high>\n" +
                "      <low>低温 2℃</low>\n" +
                "      <day>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>西南风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </day>\n" +
                "      <night>\n" +
                "        <type>晴</type>\n" +
                "        <fengxiang>北风</fengxiang>\n" +
                "        <fengli><![CDATA[<3级]]></fengli>\n" +
                "      </night>\n" +
                "    </weather>\n" +
                "  </forecast>\n" +
                "</resp>\n" +
                "<!-- 10.42.161.121(10.42.161.121):8180 ; 10.42.161.121:8080 -->\n";
        Document document = null;
        try {
            document = DocumentHelper.parseText(resultStr);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootElement = document.getRootElement();
        Element environment = rootElement.element("environment");
        String wendu = rootElement.elementText("wendu");
        String quality = environment.elementTextTrim("quality");
        String pm25 = environment.elementTextTrim("pm25");
        Element forecast = rootElement.element("forecast");
        List<Element> list = forecast.elements();
        Element weather = list.get(1);
        String type = weather.element("day").elementText("type");
        String fengxiang = weather.element("day").elementText("fengxiang");
        String date = weather.elementText("date");
        System.out.println("温度:"+wendu+"  空气质量:"+quality+
        "   pm2.5:"+pm25 +"   天气:"+type+"  风向:"+fengxiang +
        "  日期:"+date);
    }
}

运行结果:

原文发布于微信公众号 - 国产程序员(Monday_lida)

原文发表时间:2018-10-28

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券