Android获取百度天气接口
前些天知道了如何获取数字签名SHA1,那它用来干嘛的呢?今天就来学习一下它的用处。
比如说我们要开发一款基于位置的服务,如地图、天气预报等。我们要用到API Key。
百度API Key申请地址:http://lbsyun.baidu.com/apiconsole/key
创建应用 如图:
提交后得到API Key 如图:
接口实例:http://api.map.baidu.com/telematics/v3/weather?location=南昌&output=json&ak=你的API Key&mcode=你的数字签名SHA1;com.example.administrator.jsontest(包名)
接口参数说明
参数类型 | 参数名称 | 是否必须 | 具体描述 |
---|---|---|---|
String | location | true |
输入城市名或经纬度,城市名如北京或者131,经纬度格式为lng,lat坐标,如:location=116.305145,39.982368;全国值为all,返回省会城市自治区,港澳台天气情况多城市天气预报中间用“|”分隔,如:location=116.305145,39.982368|123.342323,36238945|... |
String | output | false | 输出的数据格式,默认为xml格式,当output设置为json时,输出的为json数据格式 |
String | coord_type | false | 请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84; |
返回的JSON数据
{
"error":0,
"status":"success",
"date":"2016-03-05",
"results":[
{
"currentCity":"北京",
"pm25":"144",
"index":[
{
"title":"穿衣",
"zs":"较冷",
"tipt":"穿衣指数",
"des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},
{
"title":"洗车",
"zs":"不宜",
"tipt":"洗车指数",
"des":"不宜洗车,未来24小时内有扬沙或浮尘,如果在此期间洗车,极易很快蒙上新的灰尘。"},
{
"title":"旅游",
"zs":"一般",
"tipt":"旅游指数",
"des":"风稍大,扬沙或浮尘天气对能见度和空气质量都会有些影响,出行请注意交通安全和采取适当的防尘措施。"},
{
"title":"感冒",
"zs":"易发",
"tipt":"感冒指数",
"des":"昼夜温差大,风力较强,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},
{
"title":"运动",
"zs":"较不宜",
"tipt":"运动指数",
"des":"有扬沙或浮尘,建议适当停止户外运动,选择在室内进行运动,以避免吸入更多沙尘,有损健康。"},
{
"title":"紫外线强度",
"zs":"最弱",
"tipt":"紫外线强度指数",
"des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}
],
"weather_data":[
{
"date":"周六 03月05日 (实时:12℃)", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/fuchen.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png",
"weather":"浮尘转晴",
"wind":"北风4-5级",
"temperature":"12 ~ -1℃"},
{
"date":"周日",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"多云",
"wind":"微风",
"temperature":"10 ~ -3℃"},
{
"date":"周一", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png",
"weather":"多云转阴",
"wind":"微风",
"temperature":"13 ~ 2℃"},
{
"date":"周二", "dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"阴转多云",
"wind":"北风3-4级",
"temperature":"6 ~ -1℃"}
]}]}
数据解析
public class WeatherData {
public String date;
public String error;
public List<Results> results;
public String status;
public class Results{
public String currentCity;
public List<Index> index;
public String pm25;
public List<Weather_data> weather_data;
}
public class Index{
public String des;
public String tipt;
public String title;
public String zs;
}
public class Weather_data{
public String date;
public String dayPictureUrl;
public String nightPictureUrl;
public String temperature;
public String weather;
public String wind;
}
}