原文連接:http://www.sojson.com/blog/23...
穩定高效的天氣API接口是不多有的,本站前一段時間開發了一個萬年曆,須要帶天氣預報,花了幾天功夫,集成和容錯處理好對外開放的天氣接口。天氣接口支持返回JSON和XML結果。
而且能夠獲取到昨天,以及今天和將來4 天的天氣預報。那麼下面直接介紹天氣預報的接口吧。html
天氣API詳情介紹 在 API 中心:http://www.sojson.com/api/wea... ,下面只介紹下調用方式,而後 用 Java 語言調用演示一下。java
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); }
{ "message": "Success !", "status": 200, "city": "北京", "count": 1, "data": { "shidu": "71%", "pm25": 51, "pm10": 66, "quality": "良", "wendu": "28", "ganmao": "極少數敏感人羣應減小戶外活動", "yesterday": { "date": "09日星期三", "sunrise": "05:19", "high": "高溫 30.0℃", "low": "低溫 23.0℃", "sunset": "19:20", "aqi": 79, "fx": "北風", "fl": "<3級", "type": "雷陣雨", "notice": "雷雨閃電時,應切斷電器電源,以避免損壞" }, "forecast": [ { "date": "10日星期四", "sunrise": "05:20", "high": "高溫 32.0℃", "low": "低溫 24.0℃", "sunset": "19:19", "aqi": 60, "fx": "南風", "fl": "<3級", "type": "多雲", "notice": "綿綿的雲朵,形狀變幻無窮" }, { "date": "11日星期五", "sunrise": "05:21", "high": "高溫 33.0℃", "low": "低溫 24.0℃", "sunset": "19:17", "aqi": 97, "fx": "東南風", "fl": "<3級", "type": "陰", "notice": "陰天是彩虹的前期之景" }, { "date": "12日星期六", "sunrise": "05:22", "high": "高溫 30.0℃", "low": "低溫 23.0℃", "sunset": "19:16", "aqi": 65, "fx": "南風", "fl": "<3級", "type": "雷陣雨", "notice": "雷雨較大時要遠離樹木,選擇建築物躲雨" }, { "date": "13日星期日", "sunrise": "05:23", "high": "高溫 26.0℃", "low": "低溫 22.0℃", "sunset": "19:15", "aqi": 52, "fx": "北風", "fl": "<3級", "type": "雷陣雨", "notice": "空曠場地不要使用有金屬尖端的雨傘" }, { "date": "14日星期一", "sunrise": "05:24", "high": "高溫 30.0℃", "low": "低溫 24.0℃", "sunset": "19:13", "aqi": 43, "fx": "北風", "fl": "<3級", "type": "陰", "notice": "陰天是彩虹的前期之景" } ] } }
注意的一點就是,參數中文要轉碼。要url轉碼。 Java 以下:apache
//參數url化 String city = java.net.URLEncoder.encode("北京", "utf-8");
還有一點就是,編碼爲UTF-8json
本站使用的Demo,是在萬年曆功能使用,下面是截圖。
萬年曆:http://www.sojson.com/time/wa...
若是須要元素圖片,也就是對應天氣的素材圖片,請加QQ羣(259217951),而後回覆 「天氣圖片」,會有機器人回覆您,給你圖片的下載地址,以及圖片的對應含義。 JSON 格式的。api