[ActionScript 3.0] AS3调用百度天气预报查询API

接口说明

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

接口示例

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格式数据
<?xml version="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
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.HTTPStatusEvent;
    import flash.events.IOErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    
    /**
     * @author Frost.Yen
     * @E-mail 871979853@qq.com
     * @create 2015-10-20 下午3:44:25
     *
     */
    public class BaiduWeatherTest extends Sprite
    {
        private var _location:String="成都";//城市名称或者经纬度(如116.305145,39.982368)
        private var _webURL:String = "http://api.map.baidu.com/telematics/v3/weather";
        private var _ak:String = "ncp4Qv2GGk8XWxCFBo1fOAzt";//百度ak申请地址:http://lbsyun.baidu.com/apiconsole/key
        private var _dataType:String=""//默认返回xml格式,如果&output=json则为json格式
        private var _ldr:URLLoader = new URLLoader();
        public function BaiduWeatherTest()
        {
            initViews();
            initEventListeners();
        }
        private function initViews():void
        {
            loadWeather();
        }
        private function initEventListeners():void
        {
            _ldr.addEventListener(Event.COMPLETE,onWeatherLoaded);
            _ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS,onStatus)
            _ldr.addEventListener(IOErrorEvent.IO_ERROR,onError);
            
        }
        private function loadWeather():void
        {
            //完整地址示例:http://api.map.baidu.com/telematics/v3/weather?location=成都&output=json&ak=D445c65d79c46492e829a2a6aaba0816&qq-pf-to=pcqq.c2c
            _ldr.load(new URLRequest(_webURL+"?location="+encodeURI(_location)+_dataType+"&ak="+_ak));
        }
        private function onWeatherLoaded(e:Event):void
        {
            trace(e.target.data);
            if(_dataType=="&output=json"){
                //json格式
                var obj:Object = JSON.parse(e.target.data as String);
                parseWeather(obj);
            }else if(_dataType == ""){
                //xml格式
                var xml:XML = XML(e.target.data);
                //trace(xml);
            }
        }
        private function onStatus(e:HTTPStatusEvent):void
        {
            trace("status:"+e.status);
        }
        private function onError(e:IOErrorEvent):void
        {
            trace("error:"+e.toString());
        }
        private function parseWeather(data:Object):void
        {
            if(!data.hasOwnProperty("results")){
                trace("没有查询到数据,请重试...");
                loadWeather();
                return;
            }
            var weatherData:Array = [];//天气状况数据    
            for(var key:String in data){
                //trace("---"+key,":",obj[key]+"
");
                if(data[key] is Array){
                    for(var i:int = 0;i<data[key].length;i++){
                        if(data[key][i] is Object){
                            for(var key1:String in data[key][i]){
                                //trace("***"+key1,":",obj[key][i][key1]+"
");
                                if(key1=="weather_data"){
                                    weatherData = data[key][i][key1];
                                }
                            }
                        }
                    }
                }
            }
            
        }
    }
}

原文地址:https://www.cnblogs.com/frost-yen/p/4895309.html