Flex遠程訪問獲取數據--HTTPService

編寫service類:json

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中引入:ide

  

<fx:Declarations>
        <!-- 將非可視元素(例如服務、值對象)放在此處 -->
        <services:CategoryService id="categoryService"/>
    </fx:Declarations>

在方法中調用:flex

<?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>
相關文章
相關標籤/搜索