2010-09-10 92 views
0

我正在將AMFPHP 1.9,ActionScript 3和Adobe Flash Pro CS5與Flash player 10x一起使用。我試圖通過調用amfphp服務來獲取數據。我正在Flash IDE中運行應用程序。AMFPHP:無法將PHP類映射到AS3類

對於類似字符串或整數的原始類型,它工作正常。但是當試圖獲取自定義對象時,它顯示以下錯誤:

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

@daidai:非常感謝您的回覆。我認爲這篇文章將永遠留在SOF數據庫內部未回答/未答覆,並且永遠看不到光。然後,經過十多天,你答覆了:)我提高了你的答案,因爲你關心的是回覆。再次感謝。

我也從瀏覽器測試過它。同樣的結果。

但我終於明白了爲什麼它幾天前沒有工作。問題是,出於一些奇怪的(或安全)原因,從MovieClip繼承的自定義AS3類(以及我猜測所有系統類)無法從他們的PHP對象(在我的情況下,來自Scene.php類)中映射。 但是AS3類可以發送到PHP服務類並被成功映射。 我不得不對我的體系結構進行一些更改以解決此問題並解決了我的問題。但是這個問題真的讓我處於邊緣地位,我把這篇文章留在小時候看幾天,希望得到一些回覆。

無論如何,我只測試它在Flash播放器版本10.1.82.76。我不知道它是否適用於以前的版本,因爲我沒有對它們進行測試。

3

嘗試從瀏覽器中運行,我發現AMFPHP/Flash在IDE內部很有趣。