標籤:
百度提供天氣預報查詢介面API,可以根據經緯度/城市名查詢天氣情況。
介面說明
根據經緯度/城市名查詢天氣的結果
介面樣本
http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey
百度ak申請地址:http://lbsyun.baidu.com/apiconsole/key
AK申請到以後,就可以直接用這個介面了。
介面參數說明
| 參數類型 |
參數名稱 |
是否必須 |
具體描述 |
| String |
ak |
true |
開發人員密鑰 |
| String |
sn |
false |
若使用者所用ak的校正方式為sn校正時該參數必須。 |
| String |
location |
true |
輸入城市名或經緯度,城市名稱如:北京,經緯度格式為lng,lat座標如: location=116.305145,39.982368;城市天氣預報中間"|"分 隔,location=116.305145,39.982368| 122.305145,36.982368|…. |
| String |
output |
false |
輸出的資料格式,預設為xml格式,當output設定為’json’時,輸出的為json格式的資料; |
| String |
coord_type |
false |
請求參數座標類型,預設為gcj02經緯度座標。允許的值為bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度經緯度座標,bd09mc表示百度墨卡托座標,gcj02表示經過國測局加密的座標。wgs84表示gps擷取的座標。 |
返回結果
| 參數名稱 |
含義 |
說明 |
| currentCity |
當前城市 |
返回城市名 |
| status |
返回結果狀態資訊 |
|
| date |
目前時間 |
年-月-日 |
| results |
天氣預報資訊 |
白天可返回近期3天的天氣情況(今天、明天、後天)、晚上可返回近期4天的天氣情況(今天、明天、後天、大後天) |
| results.currentCity |
當前城市 |
|
| results.weather_data |
weather_data.date |
天氣預報時間 |
|
| weather_data.dayPictureUrl |
白天的天氣預報圖片url |
|
| weather_data.nightPictureUrl |
晚上的天氣預報圖片url |
|
| weather_data.weather |
天氣狀況 |
所有天氣情況(”|”分隔字元):晴|多雲|陰|陣雨|雷陣雨|雷陣雨伴有冰雹|雨夾雪|小雨|中雨|大雨|暴雨|大暴雨|特大暴雨|陣雪|小雪| 中雪|大雪|暴雪|霧|凍雨|沙塵暴|小雨轉中雨|中雨轉大雨|大雨轉暴雨|暴雨轉大暴雨|大暴雨轉特大暴雨|小雪轉中雪|中雪轉大雪|大雪轉暴雪|浮 塵|揚沙|強沙塵暴|霾 |
| weather_data.wind |
風力 |
|
| weather_data.temperature |
溫度 |
|
2.在網頁上查看資料介面的資料
XML格式的資料:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <CityWeatherResponse> 3 <status>success</status> 4 <date>2014-05-03</date> 5 <results> 6 7 <currentCity>北京</currentCity> 8 <weather_data> 9 <date>周六(今天, 即時:22℃)</date>10 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/leizhenyu.png</dayPictureUrl>11 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>12 <weather>雷陣雨轉多雲</weather>13 <wind>北風5-6級</wind>14 <temperature>24 ~ 11℃</temperature>15 <date>周日</date>16 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>17 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>18 <weather>多雲轉晴</weather>19 <wind>北風4-5級</wind>20 <temperature>19 ~ 8℃</temperature>21 <date>周一</date>22 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>23 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>24 <weather>晴</weather>25 <wind>微風</wind>26 <temperature>21 ~ 9℃</temperature>27 <date>周二</date>28 <dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>29 <nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>30 <weather>多雲轉晴</weather>31 <wind>微風</wind>32 <temperature>21 ~ 10℃</temperature>33 </weather_data>34 </results>35 </CityWeatherResponse>
JSON形式的資料:
{ "error": 0, "status": "success", "date": "2014-05-03", "results": [ { "currentCity": "北京", "weather_data": [ { "date": "周六(今天, 即時:22℃)", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/leizhenyu.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png", "weather": "雷陣雨轉多雲", "wind": "北風5-6級", "temperature": "24 ~ 11℃" }, { "date": "周日", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png", "weather": "多雲轉晴", "wind": "北風4-5級", "temperature": "19 ~ 8℃" }, { "date": "周一", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png", "weather": "晴", "wind": "微風", "temperature": "21 ~ 9℃" }, { "date": "周二", "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png", "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png", "weather": "多雲轉晴", "wind": "微風", "temperature": "21 ~ 10℃" } ] } ]}
下面用一個demo來擷取天氣的資料(以擷取json為例 就不解析了):
手機上擷取到的資料:部分代碼:
1 public class MainActivity extends Activity { 2 3 private TextView showTextView; 4 5 private AsyncHttpClient mAsyncHttpClient; 6 7 private String Url = "你的百度天氣介面"; 8 9 @Override10 protected void onCreate(Bundle savedInstanceState) {11 super.onCreate(savedInstanceState);12 setContentView(R.layout.activity_main);13 14 showTextView = (TextView) findViewById(R.id.showTextView);15 16 mAsyncHttpClient = new AsyncHttpClient();17 18 mAsyncHttpClient.get(Url, null, new JsonHttpResponseHandler() {19 20 @Override21 public void onSuccess(JSONObject result) {22 super.onSuccess(result);23 24 showTextView.setText(result.toString());25 }26 27 @Override28 public void onFinish() {29 // TODO Auto-generated method stub30 super.onFinish();31 }32 33 });34 35 }36 }
android 百度天氣介面