免費天氣API,全國天氣 JSON API介面,可以獲取五天的天氣預報
阿新 • • 發佈:2019-02-07
穩定高效的天氣API介面是很少有的,本站前一段時間開發了一個萬年曆,需要帶天氣預報,花了幾天功夫,整合和容錯處理好對外開放的天氣介面。天氣介面支援返回 JSON
和
XML 結果。並且可以獲取到昨天,以及今天和未來4
天的天氣預報。那麼下面直接介紹天氣預報的介面吧。
天氣預報 API 介面 JSON返回
public static void json() throws Exception{ //引數url化 String city = java.net.URLEncoder.encode("北京", "utf-8"); //拼地址 String apiUrl = String.format("http://www.sojson.com/open/api/weather/json.shtml?city=%s",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 返回值:
{ "data": { "yesterday": { "date": "26日星期五", "high": "高溫 31℃", "fx": "南風", "low": "低溫 17℃", "fl": "微風", "type": "晴" }, "city": "北京", "aqi": "83", "forecast": [ { "date": "27日星期六", "high": "高溫 34℃", "fengli": "微風級", "low": "低溫 21℃", "fengxiang": "西南風", "type": "晴" }, { "date": "28日星期天", "high": "高溫 36℃", "fengli": "3-4級", "low": "低溫 22℃", "fengxiang": "北風", "type": "多雲" }, { "date": "29日星期一", "high": "高溫 29℃", "fengli": "微風級", "low": "低溫 18℃", "fengxiang": "東風", "type": "陰" }, { "date": "30日星期二", "high": "高溫 30℃", "fengli": "微風級", "low": "低溫 18℃", "fengxiang": "南風", "type": "陰" }, { "date": "31日星期三", "high": "高溫 32℃", "fengli": "3-4級", "low": "低溫 20℃", "fengxiang": "南風", "type": "多雲" } ], "ganmao": "各項氣象條件適宜,發生感冒機率較低。但請避免長期處於空調房間中,以防感冒。", "wendu": "27" }, "status": 200, "message": "OK" }
天氣預報 API XML返回方式
public static void xml() throws Exception{ //引數url化 String city = java.net.URLEncoder.encode("北京", "utf-8"); //拼地址 String apiUrl = String.format("http://www.sojson.com/open/api/weather/xml.shtml?city=%s",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); }
天氣預報 XML返回值:
<?xml version="1.0" encoding="UTF-8"?>
<resp>
<city>北京</city>
<updatetime>13:45</updatetime>
<wendu>27</wendu>
<fengli>3級</fengli>
<shidu>38%</shidu>
<fengxiang>東風</fengxiang>
<sunrise_1>04:51</sunrise_1>
<sunset_1>19:33</sunset_1>
<sunrise_2></sunrise_2>
<sunset_2></sunset_2>
<environment>
<aqi>83</aqi>
<pm25>61</pm25>
<suggest>極少數敏感人群應減少戶外活動</suggest>
<quality>良</quality>
<MajorPollutants>顆粒物(PM2.5)</MajorPollutants>
<o3>148</o3>
<co>1</co>
<pm10>106</pm10>
<so2>27</so2>
<no2>47</no2>
<time>13:00:00</time>
</environment>
<yesterday>
<date_1>26日星期五</date_1>
<high_1>高溫 31℃</high_1>
<low_1>低溫 17℃</low_1>
<day_1>
<type_1>晴</type_1>
<fx_1>南風</fx_1>
<fl_1>微風</fl_1>
</day_1>
<night_1>
<type_1>晴</type_1>
<fx_1>北風</fx_1>
<fl_1>微風</fl_1>
</night_1>
</yesterday>
<forecast>
<weather>
<date>27日星期六</date>
<high>高溫 34℃</high>
<low>低溫 21℃</low>
<day>
<type>晴</type>
<fengxiang>西南風</fengxiang>
<fengli>微風級</fengli>
</day>
<night>
<type>晴</type>
<fengxiang>北風</fengxiang>
<fengli>微風級</fengli>
</night>
</weather>
<weather>
<date>28日星期天</date>
<high>高溫 36℃</high>
<low>低溫 22℃</low>
<day>
<type>多雲</type>
<fengxiang>北風</fengxiang>
<fengli>3-4級</fengli>
</day>
<night>
<type>陰</type>
<fengxiang>東北風</fengxiang>
<fengli>微風級</fengli>
</night>
</weather>
<weather>
<date>29日星期一</date>
<high>高溫 29℃</high>
<low>低溫 18℃</low>
<day>
<type>陰</type>
<fengxiang>東風</fengxiang>
<fengli>微風級</fengli>
</day>
<night>
<type>陣雨</type>
<fengxiang>南風</fengxiang>
<fengli>微風級</fengli>
</night>
</weather>
<weather>
<date>30日星期二</date>
<high>高溫 30℃</high>
<low>低溫 18℃</low>
<day>
<type>陰</type>
<fengxiang>南風</fengxiang>
<fengli>微風級</fengli>
</day>
<night>
<type>多雲</type>
<fengxiang>北風</fengxiang>
<fengli>微風級</fengli>
</night>
</weather>
<weather>
<date>31日星期三</date>
<high>高溫 32℃</high>
<low>低溫 20℃</low>
<day>
<type>多雲</type>
<fengxiang>南風</fengxiang>
<fengli>3-4級</fengli>
</day>
<night>
<type>多雲</type>
<fengxiang>南風</fengxiang>
<fengli>3-4級</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>紫外線輻射極強,建議塗擦SPF20以上、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.162.42(10.10.162.42):38553 ; 10.10.156.163:8080 -->
注意的一點就是,引數中文要轉碼。要url轉碼。 Java 如下:
//引數url化
String city = java.net.URLEncoder.encode("北京", "utf-8");
還有一點就是,編碼為UTF-8
本站使用的Demo,是在萬年曆功能使用,下面是截圖。