编写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>