使用小米天氣API獲取天氣信息


1. URL部分

以下url中“%s”代表的是城市Id,比如北京的cityId=101010100;

//獲取未來五天預報信息,紅色部分信息不需要

WEATHER_DATA_URL_JSON = "http://weatherapi.market.xiaomi.com/wtr-v2/temp/forecast?cityId=%s";

返回信息格式如下:

復制代碼
{
    "weatherinfo": { "city": "北京", "city_en": "beijing", "cityid": "101010100", "date": "", "date_y": "2014年04月08日", "fchh": "18", "fl1": "3-4級轉4-5級", "fl2": "3-4級轉小於3級", "fl3": "小於3級", "fl4": "小於3級", "fl5": "小於3級", "fl6": "微風", "fx1": "南風轉北風", "fx2": "東北風轉無持續風向", 
"img1": "", // img信息不需要 "img10": "", "img11": "", "img12": "", "img2": "", "img3": "", "img4": "", "img5": "", "img6": "", "img7": "", "img8": "", "img9": "",
"img_single": "", //img_title信息不需要 "img_title1": "霾", "img_title10": "多雲", "img_title11": "晴", "img_title12": "晴", "img_title2": "多雲", "img_title3": "晴", "img_title4": "多雲", "img_title5": "多雲", "img_title6": "陰", "img_title7": "陰", "img_title8": "多雲", "img_title9": "晴", "img_title_single": "",
"index": "較舒適", "index48": "", "index48_d": "", "index48_uv": "", "index_ag": "極易發", "index_cl": "不宜", "index_co": "舒適", "index_d": "", "index_ls": "不宜", "index_tr": "適宜", "index_uv": "最弱", "index_xc": "不宜",
"st1": "", //st信息不需要(暫不確定其作用) "st2": "", "st3": "", "st4": "", "st5": "", "st6": "",
"temp1": "12℃~28℃", "temp2": "11℃~18℃", "temp3": "8℃~18℃", "temp4": "9℃~22℃", "temp5": "10℃~22℃", "temp6": "0℃~0℃",
"tempF1": "", //華氏攝氏度信息不需要 "tempF2": "", "tempF3": "", "tempF4": "", "tempF5": "", "tempF6": "",
"weather1": "霾轉多雲", "weather2": "晴轉多雲", "weather3": "多雲轉陰", "weather4": "陰轉多雲", "weather5": "晴轉多雲", "weather6": "晴", "week": "星期二", "wind1": "南風轉北風", "wind2": "東北風轉無持續風向", "wind3": "微風", "wind4": "微風", "wind5": "微風", "wind6": "微風" } }
復制代碼

//獲取實時天氣信息

WEATHER_DATA_SK_JSON = "http://weatherapi.market.xiaomi.com/wtr-v2/temp/realtime?cityId=%s";

返回信息格式如下:

復制代碼
{
    "weatherinfo": { "SD": "32%", "WD": "西南風", "WS": "2級", "WSE": "", "city": "北京", "cityid": "101010100", "isRadar": "1", "radar": "JC_RADAR_AZ9010_JB", "temp": "23", "time": "18:35", "weather": "霾" } }
復制代碼

//獲取其他指數類天氣信息,如pm2.5,二氧化硫含量等,暫時不需要這類信息

WEATHER_AQI_URL_JSON = "http://weatherapi.market.xiaomi.com/wtr/data/aqi?city_id=%s";

返回信息格式如下:

復制代碼
{//不需要
    "aqi": {
        "city": "北京", 
        "city_id": "101010100", 
        "pub_time": "2014-04-08 18:00", 
        "aqi": "206", 
        "pm25": "156", 
        "pm10": "196", 
        "so2": "47", 
        "no2": "42", 
        "src": "中國環境監測總站", 
        "spot": ""
    }
}
復制代碼

獲取警告信息

WEATHER_ALERT_URL_JSON = "http://weatherapi.market.xiaomi.com/wtr-v2/temp/alert?cityId=%s";

一般返回為空,且我們暫時用不到這類信息。

2. IP地址和cityId數據庫

根據天氣設計需求,要實現根據用戶的IP地址自動匹配用戶所在的當前城市。

首先要根據IP地址獲取城市名稱,其次通過城市名城獲取對應的cityId,最后利用cityId獲取對應城市的天氣信息。

因此需要兩個數據庫,包含兩張表:

表一: IP地址-->城市名稱

表二: 城市名城-->cityId,對應的數據庫文件分別是:ip_address_jj_city_name.db和city.db。

3. 數據返回格式極其他

要求數據返回格式為json


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM