首页 > 代码库 > android 百度天气接口

android 百度天气接口

百度提供天气预报查询接口API,可以根据经纬度/城市名查询天气情况。

接口说明

根据经纬度/城市名查询天气的结果

接口示例

http://api.map.baidu.com/telematics/v3/weather?location=北京&output=json&ak=yourkey
百度ak申请地址:http://lbsyun.baidu.com/apiconsole/key
AK申请到以后,就可以直接用这个接口了。

接口参数说明

参数类型参数名称是否必须具体描述
Stringaktrue开发者密钥
Stringsnfalse若用户所用ak的校验方式为sn校验时该参数必须。 
Stringlocationtrue输入城市名或经纬度,城市名称如:北京,经纬度格式为lng,lat坐标如: location=116.305145,39.982368;城市天气预报中间"|"分 隔,location=116.305145,39.982368| 122.305145,36.982368|….
Stringoutputfalse输出的数据格式,默认为xml格式,当output设置为’json’时,输出的为json格式的数据;
Stringcoord_typefalse请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84。bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托坐标,gcj02表示经过国测局加密的坐标。wgs84表示gps获取的坐标。

返回结果

参数名称含义说明
currentCity当前城市返回城市名
status返回结果状态信息 
date当前时间年-月-日
results天气预报信息白天可返回近期3天的天气情况(今天、明天、后天)、晚上可返回近期4天的天气情况(今天、明天、后天、大后天)
results.currentCity当前城市 
results.weather_dataweather_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 百度天气接口