[ActionScript 3.0] AS3调用百度天气预报查询API
2015-10-20 16:55
337 查看
接口说明
根据经纬度/城市名查询天气的结果接口示例
http://api.map.baidu.com/telematics/v3/weather?location=成都&output=json&ak=yourkey
百度ak申请地址:http://lbsyun.baidu.com/apiconsole/key
接口参数说明
参数类型 | 参数名称 | 是否必须 | 具体描述 |
---|---|---|---|
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 | 温度 |
返回的xml格式数据
<?xmlversion="1.0"encoding="utf-8"?>
<CityWeatherResponse>
<error>0</error>
<status>success</status>
<date>2015-10-20</date>
<results>
<currentCity>成都</currentCity>
<weather_data>
<date>周二10月20日(实时:25℃)</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/yin.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
<weather>阴转多云</weather>
<wind>南风微风</wind>
<temperature>25~18℃</temperature>
<date>周三</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/yin.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/xiaoyu.png</nightPictureUrl>
<weather>阴转小雨</weather>
<wind>南风微风</wind>
<temperature>24~18℃</temperature>
<date>周四</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/xiaoyu.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/xiaoyu.png</nightPictureUrl>
<weather>小雨</weather>
<wind>北风微风</wind>
<temperature>22~17℃</temperature>
<date>周五</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/xiaoyu.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/xiaoyu.png</nightPictureUrl>
<weather>小雨</weather>
<wind>北风微风</wind>
<temperature>21~16℃</temperature>
</weather_data>
<index>
<title>穿衣</title>
<zs>舒适</zs>
<tipt>穿衣指数</tipt>
<des>建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。</des>
<title>洗车</title>
<zs>较适宜</zs>
<tipt>洗车指数</tipt>
<des>较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。</des>
<title>旅游</title>
<zs>适宜</zs>
<tipt>旅游指数</tipt>
<des>天气较好,温度适宜,总体来说还是好天气哦,这样的天气适宜旅游,您可以尽情地享受大自然的风光。</des>
<title>感冒</title>
<zs>较易发</zs>
<tipt>感冒指数</tipt>
<des>昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。</des>
<title>运动</title>
<zs>较适宜</zs>
<tipt>运动指数</tipt>
<des>阴天,较适宜进行各种户内外运动。</des>
<title>紫外线强度</title>
<zs>最弱</zs>
<tipt>紫外线强度指数</tipt>
<des>属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。</des>
</index>
<pm25>147</pm25>
</results>
</CityWeatherResponse>
返回的JSON格式的数据:
{
"error":0,
"status":"success",
"date":"2015-10-20",
"results":[
{
"currentCity":"成都",
"pm25":"152",
"index":[
{
"title":"穿衣",
"zs":"舒适",
"tipt":"穿衣指数",
"des":"建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
},
{
"title":"洗车",
"zs":"较适宜",
"tipt":"洗车指数",
"des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
},
{
"title":"旅游",
"zs":"适宜",
"tipt":"旅游指数",
"des":"天气较好,温度适宜,总体来说还是好天气哦,这样的天气适宜旅游,您可以尽情地享受大自然的风光。"
},
{
"title":"感冒",
"zs":"较易发",
"tipt":"感冒指数",
"des":"昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。"
},
{
"title":"运动",
"zs":"较适宜",
"tipt":"运动指数",
"des":"阴天,较适宜进行各种户内外运动。"
},
{
"title":"紫外线强度",
"zs":"最弱",
"tipt":"紫外线强度指数",
"des":"属弱紫���线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
}
],
"weather_data":[
{
"date":"周二10月20日(实时:25℃)",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather":"阴转多云",
"wind":"南风微风",
"temperature":"25~18℃"
},
{
"date":"周三",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/yin.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"weather":"阴转小雨",
"wind":"南风微风",
"temperature":"24~18℃"
},
{
"date":"周四",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"weather":"小雨",
"wind":"北风微风",
"temperature":"22~17℃"
},
{
"date":"周五",
"dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png",
"nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png",
"weather":"小雨",
"wind":"北风微风",
"temperature":"21~16℃"
}
]
}
]
}
以下是AS3代码示例:
package
{
importflash.display.Sprite;
importflash.events.Event;
importflash.events.HTTPStatusEvent;
importflash.events.IOErrorEvent;
importflash.net.URLLoader;
importflash.net.URLRequest;
/**
*@authorFrost.Yen
*@E-mail871979853@qq.com
*@create2015-10-20下午3:44:25
*
*/
publicclassBaiduWeatherTestextendsSprite
{
privatevar_location:String="成都";//城市名称或者经纬度(如116.305145,39.982368)
privatevar_webURL:String="http://api.map.baidu.com/telematics/v3/weather";
privatevar_ak:String="ncp4Qv2GGk8XWxCFBo1fOAzt";//百度ak申请地址:http://lbsyun.baidu.com/apiconsole/key
privatevar_dataType:String=""//默认返回xml格式,如果&output=json则为json格式
privatevar_ldr:URLLoader=newURLLoader();
publicfunctionBaiduWeatherTest()
{
initViews();
initEventListeners();
}
privatefunctioninitViews():void
{
loadWeather();
}
privatefunctioninitEventListeners():void
{
_ldr.addEventListener(Event.COMPLETE,onWeatherLoaded);
_ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS,onStatus)
_ldr.addEventListener(IOErrorEvent.IO_ERROR,onError);
}
privatefunctionloadWeather():void
{
//完整地址示例:http://api.map.baidu.com/telematics/v3/weather?location=成都&output=json&ak=D445c65d79c46492e829a2a6aaba0816&qq-pf-to=pcqq.c2c
_ldr.load(newURLRequest(_webURL+"?location="+encodeURI(_location)+_dataType+"&ak="+_ak));
}
privatefunctiononWeatherLoaded(e:Event):void
{
trace(e.target.data);
if(_dataType=="&output=json"){
//json格式
varobj:Object=JSON.parse(e.target.dataasString);
parseWeather(obj);
}elseif(_dataType==""){
//xml格式
varxml:XML=XML(e.target.data);
//trace(xml);
}
}
privatefunctiononStatus(e:HTTPStatusEvent):void
{
trace("status:"+e.status);
}
privatefunctiononError(e:IOErrorEvent):void
{
trace("error:"+e.toString());
}
privatefunctionparseWeather(data:Object):void
{
if(!data.hasOwnProperty("results")){
trace("没有查询到数据,请重试...");
loadWeather();
return;
}
varweatherData:Array=[];//天气状况数据
for(varkey:Stringindata){
//trace("---"+key,":",obj[key]+"\n");
if(data[key]isArray){
for(vari:int=0;i<data[key].length;i++){
if(data[key][i]isObject){
for(varkey1:Stringindata[key][i]){
//trace("***"+key1,":",obj[key][i][key1]+"\n");
if(key1=="weather_data"){
weatherData=data[key][i][key1];
}
}
}
}
}
}
}
}
}
相关文章推荐
- php常用正则
- 第八周--字符串加密
- [selenium2]利用AUTOIT上传 下载
- Android 的数据存储方式有哪些?
- JDBC获取connection
- win7系统中重装win10系统的方法 win7系统重装win10系统图文教程
- 使用GridView实现仿桌面图标的设计
- CCNA学习笔记2---以太网
- 远程控制计算机
- php 命令行工具 日志请使用绝对路径
- iOS ---打开iPhone照相机和相册
- java学习——代理模式之动静PK
- 实现一个对象验证库系列 -- 1) 接口介绍以及总体思路概述 (请大神批评)
- android开源项目框架大全:
- 【SQL Server】左联接,右联接,内联接的比较
- C# WinForm 单例模式(例:同一个窗体只创建一次实例)
- android SeekBar双滑块
- Java 连接池的工作原理
- iOS开发笔记--超全!iOS 面试题汇总
- 带阻尼效果的scrollview,仿QQ空间