AS3的remoting不支援recordSet囉...那什麼是recordset?
在as2時:
PHP端
$query = 'SELECT * FROM `News`";
return mysql_query($query);
FLASH
function onResult(result:Result)
{
trace(result.items[i].news_title);
//就可以直接得到值了。
}
那到了AS3時,因為改採用AMF3的格式,我們會發現傳回來的資料和以前的不太一樣。
如下圖:
回傳的東西會包在 serverInfo裏。
資料會存在initialData,欄位名稱存在columnNames。
乍看之下,感覺變麻煩了。我們還得自己製作「組合」的動作。
才能像以前一樣的使用。
pdf文件:AMF 3 Specification
以下是我目前正在使用的remoting class
AS3
Remoting.as:
package
{
import flash.events.Event;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
import ArrayCollectionDP;
public class Remoting extends NetConnection
{
/*VARIABLE*/
private static var m_instance:Remoting
private static var m_constructorLocked:Boolean = false;
private var m_eventName:String;
private var m_data:Object;
/*CONSTRUCTOR*/
public function Remoting()
{
if( m_constructorLocked )
{
throw new Error( "Remoting is a singleton" );
}
}
/*PUBLIC*/
public static function GetInstance():Remoting
{
if( m_instance == null )
{
m_constructorLocked = false;
Remoting.m_instance = new Remoting();
m_constructorLocked = true;
}
return Remoting.m_instance;
}
public function Init():void
{
objectEncoding = ObjectEncoding.AMF3;
connect(yourGatewayPath);
}
public function GetResult():Object
{
return m_data;
}
public function GetData(a_funcName:String='',a_eventName:String=""):void
{
m_eventName = a_eventName
var responder:Responder = new Responder(onResult, onFault);
this.call(YouServiceName+"."+a_funcName, responder);
}
/*PRIVATE*/
private function onResult(result:Object):void
{
m_data = ArrayCollectionDP.ToObject(result);
if (m_eventName != "")
{
dispatchEvent(new Event(m_eventName));
}
}
private function onFault(fault:Object):void
{
trace(String(fault.description));
//this.dispatchEvent({type:"onDataError"});
}
}
}
ArrayCollectionDP.as:
package{
public class ArrayCollectionDP {
public static function ToObject(a_arrayCollection:Object)
{
var initialData:Array = a_arrayCollection.serverInfo.initialData;
var category:Array = a_arrayCollection.serverInfo.columnNames;
var dataAry:Array = new Array();
var i:int;
for (i = 0; i < initialData.length; i++)
{
dataAry[i] = new Object();
for (var aIndex:* in category)
{
dataAry[i][category[aIndex]] = initialData[i][aIndex];
}
}
return dataAry
}
}
}
其他class要使用Remoting時
Main.as:
package {
import Remoting;
public class Main extends MovieClip
{
public function Main()
{
Remoting.GetInstance().Init();
Remoting.GetInstance().GetData('GetExam','GetData');
Remoting.GetInstance().addEventListener('GetData', GetDataHandler)
}
private function GetDataHandler(e:Event)
{
var m_obj = m_remoting.GetResult()
}
}
提供2個Debuggers的工具。
ServiceCapture
Charles Web Debugging Proxy