天氣預報API

天氣預報API是一個針對一個地點或一個地區,查詢當前天氣編寫的一個API調用接口,一般返回為XML或者JSON格式報文,通過此接口可以獲取一個城市的溫度、氣候、最高氣溫、最低氣溫等數據的信息。

基本介紹

  • 中文名:天氣預報API
  • 外文名:Weather Open API
  • 類別:API
  • 說明:天氣預報獲取接口
程式介紹,測試工具,參數說明,調用實例,

程式介紹

天氣預報API是一個提供未來天氣預報數據的一個API調用接口,通過此接口可以獲取一個城市的溫度、氣候、PM2.5、濕度、最高最低氣溫等信息,從而套用自己編寫的程式中。

測試工具

採用 Postman 或者採用 Sojson 線上測試工具。

參數說明

只需要一個參數city名稱,可以是帶市區縣。具體參考以下表格。
參數名稱
是否必填
類型
備註
city
String
可以是city=北京,也可city=北京市
返回值說明
欄位名稱
類型
備註
yesterday
JSONObject
昨天天氣詳情
forecast
JSONArray
從今天開始的5天的天氣詳情
date
String
日期+星期,如:26日星期五
high
String
當日最高溫,如:高溫 34℃
low
String
當日最低溫 ,如:低溫 21℃
fengxiang|fx
String
風向,如:西南風
type
String
天氣描述,如:多雲
ganmao
String
對於天氣的簡短描述注意事項
wendu
String
現在的溫度
shidu
String
當前濕度
pm25
int
PM2.5
pm10
int
PM10
notice
String
對天氣的描述語
sunrise
String
當前城市的日出時間
sunset
String
當前城市的日落時間
aqi
int
空氣品質

調用實例

JSON API 調用
採用Java Http方式調用,代碼如下:
public static void json() throws Exception{    //參數url化,以北京為實例    String city = java.net.URLEncoder.encode("北京", "utf-8");     String apiUrl = "sojson 天氣API URL";    //拼地址    String apiUrl = String.format("%s?city=%s",apiUrl,city);    //開始請求    URL url= new URL(apiUrl);    URLConnection open = url.openConnection();    InputStream input = open.getInputStream();    //這裡轉換為String,帶上包名,怕你們引錯包    String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");    //輸出    System.out.println(result);}
JSON API 返回值
返回值以JSON返回,具體欄位對應的意思,參考下面的參數和返回值說明。
{    "message": "Success !",    "status": 200,    "city": "合肥",    "count": 4,    "data": {        "shidu": "92%",        "pm25": 10,        "pm10": 15,        "quality": "優",        "wendu": "27",        "ganmao": "各類人群可自由活動",        "yesterday": {            "date": "07日星期一",            "sunrise": "05:29",            "high": "高溫 37.0℃",            "low": "低溫 28.0℃",            "sunset": "19:03",            "aqi": 55,            "fx": "南風",            "fl": "<3級",            "type": "雷陣雨",            "notice": "空曠場地不要使用有金屬尖端的雨傘"        },        "forecast": [            {                "date": "08日星期二",                "sunrise": "05:30",                "high": "高溫 32.0℃",                "low": "低溫 26.0℃",                "sunset": "19:02",                "aqi": 34,                "fx": "南風",                "fl": "<3級",                "type": "中雨",                "notice": "將出現中雨,請攜帶雨具,避免濕身"            },            {                "date": "09日星期三",                "sunrise": "05:31",                "high": "高溫 32.0℃",                "low": "低溫 25.0℃",                "sunset": "19:01",                "aqi": 40,                "fx": "西南風",                "fl": "<3級",                "type": "陰",                "notice": "灰濛濛的天空就像模糊了的眼睛"            },            {                "date": "10日星期四",                "sunrise": "05:31",                "high": "高溫 35.0℃",                "low": "低溫 26.0℃",                "sunset": "19:01",                "aqi": 53,                "fx": "南風",                "fl": "<3級",                "type": "晴",                "notice": "晴空萬里,去沐浴陽光吧"            },            {                "date": "11日星期五",                "sunrise": "05:32",                "high": "高溫 35.0℃",                "low": "低溫 26.0℃",                "sunset": "19:00",                "aqi": 47,                "fx": "東南風",                "fl": "<3級",                "type": "多雲",                "notice": "綿綿的雲朵,形狀千變萬化"            },            {                "date": "12日星期六",                "sunrise": "05:33",                "high": "高溫 30.0℃",                "low": "低溫 25.0℃",                "sunset": "18:59",                "aqi": 32,                "fx": "南風",                "fl": "<3級",                "type": "中雨",                "notice": "現正下雨,地濕路滑,請小心開車"            }        ]    }}
API XML 調用
返回值直接返回XML,但是比上面的JSON返回值要詳細一點。下面採用Java HTTP調用方式。
public static void xml() throws Exception{    //參數url化    String city = java.net.URLEncoder.encode("茶陵縣", "utf-8");    String apiUrl = "sojson天氣API URL";    //拼地址    String apiUrl = String.format("%s?city=%s",apiUrl,city);    //開始請求    URL url= new URL(apiUrl);    URLConnection open = url.openConnection();    InputStream input = open.getInputStream();    //這裡轉換為String,帶上包名,怕你們引錯包    String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");    //輸出    System.out.println(result);}
JSON XML 返回值
<resp>    <city>茶陵</city>    <updatetime>13:05</updatetime>    <wendu>34</wendu>    <fengli>        <![CDATA[ 5級 ]]>    </fengli>    <shidu>52%</shidu>    <fengxiang>西風</fengxiang>    <sunrise_1>05:56</sunrise_1>    <sunset_1>19:05</sunset_1>    <sunrise_2/>    <sunset_2/>    <yesterday>        <date_1>11日星期五</date_1>        <high_1>高溫 33℃</high_1>        <low_1>低溫 26℃</low_1>        <day_1>            <type_1>陣雨</type_1>            <fx_1>南風</fx_1>            <fl_1>                <![CDATA[ <3級 ]]>            </fl_1>        </day_1>        <night_1>            <type_1>多雲</type_1>            <fx_1>南風</fx_1>            <fl_1>                <![CDATA[ <3級 ]]>            </fl_1>        </night_1>    </yesterday>    <forecast>        <weather>            <date>12日星期六</date>            <high>高溫 35℃</high>            <low>低溫 26℃</low>            <day>                <type>多雲</type>                <fengxiang>西南風</fengxiang>                <fengli>                    <![CDATA[ 3-4級 ]]>                </fengli>            </day>            <night>                <type>多雲</type>                <fengxiang>西南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </night>        </weather>        <weather>            <date>13日星期天</date>            <high>高溫 35℃</high>            <low>低溫 26℃</low>            <day>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ 3-4級 ]]>                </fengli>            </day>            <night>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </night>        </weather>        <weather>            <date>14日星期一</date>            <high>高溫 33℃</high>            <low>低溫 26℃</low>            <day>                <type>陣雨</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </day>            <night>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </night>        </weather>        <weather>            <date>15日星期二</date>            <high>高溫 34℃</high>            <low>低溫 26℃</low>            <day>                <type>陣雨</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </day>            <night>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </night>        </weather>        <weather>            <date>16日星期三</date>            <high>高溫 35℃</high>            <low>低溫 26℃</low>            <day>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </day>            <night>                <type>多雲</type>                <fengxiang>南風</fengxiang>                <fengli>                    <![CDATA[ <3級 ]]>                </fengli>            </night>        </weather>    </forecast>    <zhishus>        <zhishu>            <name>晨練指數</name>            <value>較適宜</value>            <detail>早晨氣象條件較適宜晨練,但風力稍大,晨練時會感覺有點涼,晨練時著裝不要過於單薄,選擇避風的地點。</detail>        </zhishu>        <zhishu>            <name>舒適度</name>            <value>較不舒適</value>            <detail>白天天氣多雲,同時會感到有些熱,不很舒適。</detail>        </zhishu>        <zhishu>            <name>穿衣指數</name>            <value>炎熱</value>            <detail>天氣炎熱,建議著短衫、短裙、短褲、薄型T恤衫等清涼夏季服裝。</detail>        </zhishu>        <zhishu>            <name>感冒指數</name>            <value>少發</value>            <detail>各項氣象條件適宜,發生感冒機率較低。但請避免長期處於空調房間中,以防感冒。</detail>        </zhishu>        <zhishu>            <name>晾曬指數</name>            <value>適宜</value>            <detail>天氣不錯,適宜晾曬。趕緊把久未見陽光的衣物搬出來吸收一下太陽的味道吧!</detail>        </zhishu>        <zhishu>            <name>旅遊指數</name>            <value>一般</value>            <detail>天氣較好,溫度高,讓人感覺熱,幸好風比較大,能緩解炎熱的天氣。外出旅遊請注意防暑降溫和防曬。</detail>        </zhishu>        <zhishu>            <name>紫外線強度</name>            <value>中等</value>            <detail>屬中等強度紫外線輻射天氣,外出時建議塗擦SPF高於15、PA+的防曬護膚品,戴帽子、太陽鏡。</detail>        </zhishu>        <zhishu>            <name>洗車指數</name>            <value>較適宜</value>            <detail>較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。</detail>        </zhishu>        <zhishu>            <name>運動指數</name>            <value>較適宜</value>            <detail>天氣較好,但風力較大,推薦您進行室內運動,若在戶外運動請注意防風。</detail>        </zhishu>        <zhishu>            <name>約會指數</name>            <value>不適宜</value>            <detail>風捲起熱浪襲來,外出約會可能會因此敗興而歸,最好在涼爽的室內約會。</detail>        </zhishu>        <zhishu>            <name>雨傘指數</name>            <value>不帶傘</value>            <detail>天氣較好,不會降水,因此您可放心出門,無須帶雨傘。</detail>        </zhishu>    </zhishus></resp><!-- 10.10.156.163(10.10.156.163):39822 ; 10.10.162.42:8080 -->

相關詞條

熱門詞條

聯絡我們