我想調用一個返回XML的外部API,並且我沒有這個問題。Actionscript 3.0,從外部類獲取xml?
後來我想重構我的代碼,並將我的API調用移動到外部API.as類,但是如何將XML返回到我的主類?
ex。
類1:
package references.campaign
{
import flash.events.Event;
import flash.xml.XMLDocument;
import references.utils.*;
import flash.display.*;
public class myCampaign extends MovieClip
{
public function myCampaign()
{
var api = new API();
trace(api.GetMakes()); //I would like this to show XML output
}
}
}
類2:
package references.utils
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.xml.XMLDocument;
public class API
{
const ApiKey:String = "ApiKEY=random_gen_key";
const URL:String = "http://api.myURL.com/";
private var _xmlDocument:XMLDocument;
public function API()
{
_xmlDocument = new LoadExternal(URL + ApiKey);
}
public function GetMakes() : XMLDocument
{
return _xmlDocument;
}
}
}
類3:
package references.utils
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.xml.XMLDocument;
public class LoadExternal
{
private var _url:String = "";
private var _bytesLoadProgress:int = 0;
private var _loaded:XMLDocument;
public function LoadExternal(url:String = "")
{
if (url != "")
{
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest(url));
}
}
public function onComplete(e:Event):void
{
_loaded = new XMLDocument(e.target.data);
trace(_loaded); //This works, but not what i'm after.
this.addChild(_loaded); // something like this????
}
}
}
希望它使一個小感?? 我是一個.NET開發人員,所以它是新的我與此事件驅動編程..
這沒有奏效,它只是返回null。這個事件在這一點上沒有提出。 – Finnbogi 2011-02-16 08:44:25
這可能是因爲加載尚未完成。你可以派遣另一個完整的事件,並在mycampaign類中聽...由於Pavel S已經建議.. – ThomasM 2011-02-16 09:57:46
我通過公開我的「Loader」來解決問題。 我會將代碼作爲新評論發佈 – Finnbogi 2011-03-02 09:10:11