• Flex远程访问获取数据--HTTPService


    编写service类:

    package services {
        import com.adobe.serialization.json.JSON;
        
        import log.LogUtil;
        
        import mx.collections.ArrayCollection;
        import mx.collections.XMLListCollection;
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;
        import mx.rpc.http.mxml.HTTPService;
        
        
        public class CategoryService extends HTTPService {
            [Bindable]
            public var categoryArray:ArrayCollection;
            public var testFlag:Boolean = true;
            
            public function CategoryService(rootURL:String=null, destination:String=null) {
                super(rootURL, destination);
                this.url = "http://192.168.1.210:8081/xxx.action";
    //url为远程请求接口
                addEventListener(ResultEvent.RESULT, handleCategoryResult);
                
            }
            
            private function handleCategoryResult(event:ResultEvent):void{
                var rawData:String =  String(event.result);
                trace(rawData);
                LogUtil.debug("CategoryService",rawData);
                var o:Object = com.adobe.serialization.json.JSON.decode(rawData);//对结果进行json转换  
                categoryArray = new ArrayCollection(o.result);
            }    
            
                    
            
        }
    }

    结果集接收之后进行json格式转换,需要导入as3corelib.swc
    在XML中引入:

      

    <fx:Declarations>
            <!-- 将非可视元素(例如服务、值对象)放在此处 -->
            <services:CategoryService id="categoryService"/>
        </fx:Declarations>

    在方法中调用:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
                 xmlns:s="library://ns.adobe.com/flex/spark" 
                 xmlns:mx="library://ns.adobe.com/flex/mx" width="342" height="60" xmlns:services="services.*" initialize="group1_initializeHandler(event)">
        <fx:Declarations>
            <!-- 将非可视元素(例如服务、值对象)放在此处 -->
            <services:CategoryService id="categoryService"/>
        </fx:Declarations>
        
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.events.FlexEvent;
                
                import spark.events.IndexChangeEvent;
                
                            
                  
                protected function group1_initializeHandler(event:FlexEvent):void
                {
                    if(categoryService.testFlag){
                        categoryService.localTest();
                    }else{
                        categoryService.send();
                    }
                    
                    
                }
                
            ]]>
        </fx:Script>
        
           <s:DropDownList id="categoryList" x="92" y="29" width="174"
                    dataProvider="{categoryService.categoryArray}"
                    labelField="name"
                    prompt="请选择"
                    change="updateSelection(event);"
                    />
        
    
        
    </s:Group>
  • 相关阅读:
    PythonStudy——epoll 模块实现异步IO模型
    MySQLStudy——Mac下MySQL 允许用户远程访问数据库
    MySQLStudy——MySQL 基础语句
    MySQLStudy——MySQL 概念
    MySQLStudy——Mac下MySQL 常用命令 启动 关闭 重启服务 查看版本
    PythonStudy——IO模型
    PythonStudy——非阻塞IO模型
    PythonStudy——多路复用IO select实现
    restfull api
    斜体菜单
  • 原文地址:https://www.cnblogs.com/sagech/p/5630986.html
Copyright © 2020-2023  润新知