我将 AMFPHP 1.9、ActionScript 3 和 Adobe Flash Pro CS5 与 Flash player 10x 一起使用。我试图通过调用 amfphp serices 来获取数据。我正在从 Flash IDE 中运行该应用程序。
对于像字符串或整数这样的原始类型,它工作正常。但是在尝试获取自定义对象时,它会显示以下错误:
SecurityError:错误 #2000:没有活动的安全上下文。
并且结果处理函数中的参数包含 null。我在这里错过了什么吗?请帮忙。
以下是我的课程:
服务上下文.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);
}
}
场景服务.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);
}
}
场景.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);
}