Actionscript 3 从ActionScript类查询httpservice
我正试图将httpservice(在AS类中定义)的结果返回到mxml应用程序。当我将所有内容都放在主mxml中时,代码本身运行良好,但我似乎无法从AS类中使其正常工作 当我在获取httpresult(myWatchedList=newxmllistcollection(myData.movie);)的行上放置断点时,我可以确认myWatchedList具有所需的值。但是,调用getWatchedList()方法时,myWatchedList始终为null 我肯定我错过了一些很明显的东西 我的AS班级:Actionscript 3 从ActionScript类查询httpservice,actionscript-3,apache-flex,mxml,Actionscript 3,Apache Flex,Mxml,我正试图将httpservice(在AS类中定义)的结果返回到mxml应用程序。当我将所有内容都放在主mxml中时,代码本身运行良好,但我似乎无法从AS类中使其正常工作 当我在获取httpresult(myWatchedList=newxmllistcollection(myData.movie);)的行上放置断点时,我可以确认myWatchedList具有所需的值。但是,调用getWatchedList()方法时,myWatchedList始终为null 我肯定我错过了一些很明显的东西 我的A
package components
{
import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class WatchList
{
[Bindable]
public var myToWatchList:XMLListCollection;
public var myWatchedList:XMLListCollection;
public function WatchList()
{
//httpservice for watched movies
var watchedList_service:HTTPService = new HTTPService();
watchedList_service.url= "http://*****/phpscripts/selectWatchedlist.php";
watchedList_service.showBusyCursor=true;
watchedList_service.resultFormat="e4x";
watchedList_service.method="POST";
//httpservice for not yet watched movies
var toWatchList_service:HTTPService = new HTTPService();
toWatchList_service.url= "http://*****/phpscripts/selectToWatchlist.php";
toWatchList_service.showBusyCursor=true;
toWatchList_service.resultFormat="e4x";
toWatchList_service.method="POST";
//listen for result
watchedList_service.addEventListener(ResultEvent.RESULT, watchedList_result);
toWatchList_service.addEventListener(ResultEvent.RESULT, toWatchList_result);
//send request to httpservice
toWatchList_service.send();
watchedList_service.send();
}
public function toWatchList_result(event:ResultEvent):void
{
//result is xml
var myData:XML = XML(event.result);
myWatchedList = new XMLListCollection(myData.movie);
}
public function watchedList_result(event:ResultEvent):void
{
var myData:XML = XML(event.result);
myToWatchList = new XMLListCollection(myData.movie);
}
public function getWatchedList():XMLListCollection {
return this.myWatchedList;
}
public function getToWatchList():XMLListCollection {
return this.myToWatchList;
}
}
}
mxml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:components="components.*"
initialize="doSend()">
<s:layout>
<s:VerticalLayout paddingBottom="20" paddingLeft="20"
paddingRight="20" paddingTop="20"/>
</s:layout>
<fx:Script>
<![CDATA[
import components.WatchList;
import mx.collections.XMLListCollection;
[Bindable]
private var myToWatchList:XMLListCollection;
private var myWatchedList:XMLListCollection;
private var watchList:components.WatchList;
protected function doSend():void
{
watchList= new components.WatchList();
//fetch the watchlists
myToWatchList = watchList.getToWatchList();
myWatchedList = watchList.getWatchedList();
//bind the watchlists to the tilelist
myToWatchList_tile.dataProvider = myToWatchList;
myWatchedList_tile.dataProvider = myWatchedList;
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
<s:Panel id="panel" width="100%" height="100%" title="Watchlist">
<s:layout>
<s:VerticalLayout paddingBottom="5" paddingLeft="20"
paddingRight="20" paddingTop="5"/>
</s:layout>
<s:Label width="20%" fontSize="17" fontWeight="bold" text="Your watched movies"/>
<mx:TileList id="myWatchedList_tile" height="360" borderVisible="false"
columnCount="6" columnWidth="200"
itemRenderer="components.TileListItemRenderer" rowCount="1" rowHeight="360"/>
<s:Label width="20%" fontSize="17" fontWeight="bold" text="Your to watch movies"/>
<mx:TileList id="myToWatchList_tile" height="360" borderVisible="false"
columnCount="6" columnWidth="200"
itemRenderer="components.TileListItemRenderer" rowCount="1" rowHeight="360" />
</s:Panel>
首先,您可以在发出请求的地方调用构造函数:
watchList= new components.WatchList();
然后,你立即调用结果:
myToWatchList = watchList.getToWatchList();
。。。您应该等待请求完成,例如:在获取结果时抛出一个事件,并生成一个等待事件的处理程序Thx,这是有效的。我现在在watchedList_result(event:ResultEvent)中发送一个事件,并在我的mxml应用程序中侦听它。