使用小米天气API获取天气信息

时间:2024-01-30 11:14:43

2014-04-08 18:49:08

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