AMFPHP:无法将PHP类映射到AS3类

时间:2010-09-10 20:04:32

标签: flash actionscript-3 amfphp flash-cs5 flashplayer-10

我正在使用AMFPHP 1.9,ActionScript 3和Adobe Flash Pro CS5以及Flash Player 10x。我试图通过调用amfphp serices来获取数据。我正在Flash IDE中运行该应用程序。

对于像字符串或整数这样的原始类型,它工作正常。但是在尝试获取自定义对象时,它会显示以下错误:

SecurityError:错误#2000:没有活动的安全上下文。

结果处理函数中的参数包含null。我在这里错过了吗?请帮忙。

以下是我的课程:

ServiceContext.as

import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.events.NetStatusEvent;

internal class ServiceContext
{
    protected var myService:NetConnection;

    public function ServiceContext():void
    {
        myService = new NetConnection();
        myService.objectEncoding = ObjectEncoding.AMF3;
        myService.connect("http://localhost/MyApp/amfphp/gateway.php");
        myService.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    }

    private function onNetStatus(event:NetStatusEvent):void
    {
        trace(event.info);            
    }
}

SceneService.as

import com.ddr.cv.model.Scene;
import flash.net.Responder;    

public class SceneService extends ServiceContext
{
    public function SceneService():void
    {
        com.ddr.cv.model.Scene.register();
    }

    public function getSceneByID(sceneID:uint, resultHandler:Function, faultHandler:Function)
    {
        var responder = new Responder(resultHandler, faultHandler);
        myService.call("com.ddr.cv.model.Scene.getSceneByID", responder, sceneID);
    }

    public function saveScene(scene:Scene, resultHandler:Function, faultHandler:Function)
    {
        var responder = new Responder(resultHandler, faultHandler);
        myService.call("Scene.saveScene", responder, scene);
    }
}

Scene.as

    import flash.display.MovieClip;
    import flash.net.registerClassAlias;

    [RemoteClass(alias="com.ddr.cv.model.Scene")]
    [Bindable]
    public class Scene extends MovieClip
    {

        private var _id:uint;
        public function get id():uint
        {
            return _id;
        }

        private var _sceneName:String;
        public function get sceneName():String
        {
            return _sceneName;
        }

        private var _imageName:String;
        public function get imageName():String
        {
            return _imageName;
        }

        private var _sceneCategoryID:int;
        public function get sceneCategoryID():int
        {
            return _sceneCategoryID;
        }

        private var _userID:String;
        public function get userID():String
        {
            return _userID;
        }

        private var _creationDate:Date;
        public function get creationDate():Date
        {
            return _creationDate;
        }

        public function Scene(id:uint = 0, sceneName:String = null, imageName:String = null, sceneCategoryID:int = 0, userID:String = null, creationDate:Date = null):void
        {
            _id = id;
            _sceneName = sceneName;
            _imageName = imageName;
            _sceneCategoryID = sceneCategoryID;
            _userID = userID;
            _creationDate = creationDate;
        }

        public static function register():void
        {
            registerClassAlias("com.ddr.cv.model.Scene", com.ddr.cv.model.Scene) ;
        }
}

这里是 Scene.php

    <?php
class Scene {

    var $id;
    var $sceneName;
    var $imageName;
    var $sceneCategoryID;
    var $userID;
    var $creationDate;

    // explicit actionscript package
    var $_explicitType = "com.ddr.cv.model.Scene";

    function Scene($id = 0, $sceneName = null, $imageName = null, $sceneCategoryID = 0, $userID = null, $creationDate = null)
    {
        $this->id = $id;
        $this->sceneName = $sceneName;
        $this->imageName = $imageName;
        $this->sceneCategoryID = $sceneCategoryID;
        $this->userID = $userID;
        $this->creationDate = $creationDate;
    }

    function getSceneByID($id){
        //creating dummy Scene object
        $scene = new Scene(1, "Test Scene", "test_scene.jpg", 6, null, null);
        return $scene;
    }

    function saveScene($scene)
    {
        //To Do:
    }
}
?>

以下是我致电服务的方式:

var sceneService:SceneService = new SceneService();
sceneService.getSceneByID(1, getSceneByID_resultHandler, getSceneByID_faultHandler);

处理程序:

    public function getSceneByID_resultHandler(scene:com.ddr.cv.model.Scene):void
    {
        //scene contains null here.
        //Shouldn't it contain the dummy Scene object from amfphp service method?
        trace("Success: " + scene);
    }

    public function getSceneByID_faultHandler(fault:Object):void
    {
        trace(">>> fault:" + fault.description);
    }

2 个答案:

答案 0 :(得分:3)

尝试在浏览器中运行,我发现AMFPHP / Flash在IDE中很有趣。

答案 1 :(得分:2)

@daidai:非常感谢您的回复。我认为这篇文章将永远深藏在SOF数据库内部,无人接听/未回复,永远不会看到光明。然后,经过10天漫长的日子,你回答:)我提高了你的答案,因为你很关心回答。再次感谢。

我也是从浏览器测试过的。结果相同。

但我终于弄明白为什么几天前它没有工作。问题是,出于一些奇怪的(或安全性)原因,继承自MovieClip的自定义AS3类(我想对于所有系统类)不能从他们的PHP对应物(在我的例子中)从Scene.php类映射。 但AS3类可以发送到PHP服务类并成功映射。 我不得不对我的架构进行一些更改来解决这个问题并解决了我的问题。但是这个问题确实让我处于优势地位,我每小时都会看到这篇文章,希望得到一些回复。

无论如何,我只在Flash播放器版本10.1.82.76上测试过它。我不知道它是否适用于以前的版本,因为我没有对它们进行测试。