我有一个主 mxml 文件和一个 phpconnect.as 文件。当我从 mxml 文件调用 phpconnect 函数时,连接工作正常,控制台中的跟踪也工作正常。但是将 phpconnect.as 文件的结果返回到 mxml 文件的最佳方法是什么。
下面是代码 main mxml Test02HomeView.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
<fx:Declarations>
<!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import comps.PHPConnect;
protected function phpConnect_btn_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var phpconnect:PHPConnect = new PHPConnect();
phpconnect.phpconnect();
}
]]>
</fx:Script>
<s:TextArea id="log_txt" x="10" y="128" height="104"/>
<s:Button id="phpConnect_btn" x="180" y="256" width="130" label="PHPConnect" click="phpConnect_btn_clickHandler(event)"/>
</s:View>
这是来自 phpconnect.as 的代码
package comps
{
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class PHPConnect
{
public function PHPConnect()
{
}
public function phpconnect():void
{
// TODO Auto-generated method stub
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest("http://localhost/file.php");
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested data.");
}
}
private function configureListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
// log_txt.text = loader.data;
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
}
}
对于使用 actionscript 和 FlashBuilder 的人来说,这可能是一个简单的问题。对我来说,解决这个问题会很有帮助。