Actionscript 3.0,从外部类获取xml?
我正在尝试调用返回 XML 的外部 API,而且这样做没有问题。
后来我想重构我的代码,并将 API 调用移至外部 API.as 类,但如何将 XML 返回到我的主类?
前任。
第 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 开发人员,因此事件驱动编程对我来说是新鲜事。
I'm trying to call an external API which returns XML, and i have no problem doing that.
Afterwards I wanted to refactor my code, and I moved my API call to an external API.as class, but how do i return the XML to my main class??
ex.
class 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
}
}
}
class 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;
}
}
}
class 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????
}
}
}
Hope it makes a little sense??
I'm a .NET developer, so it's new to me with this event-driven programming..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将 _loaded 变量设为公共(public var returned:XMLDocument),然后您就可以这样做,
尽管您可能想为变量使用其他名称...
You need to make the _loaded variable public (public var loaded:XMLDocument) and then you can do
although you might want to use other names for your variables...
您必须向加载程序提供回调例程,以便在收到数据后进行调用。
像这样的东西
public function LoadExternal(url:String = "", onComplete: Function)
你可以这样使用它
新的LoadExternal(URL + ApiKey,函数(数据:XMLDocument){_xmlDocument =数据});
You will have to provide a callback routine to your loader to call once the data is received.
Something like
public function LoadExternal(url:String = "", onComplete: Function)
And you can use it like this
new LoadExternal(URL + ApiKey, function (data:XMLDocument){_xmlDocument = data});
使用回调是一个相当简单的解决方案,如果您想使用事件驱动的方法,您应该从 EventDispatcher 继承 LoadExternal 和 API 并分派您的事件,而不是您应该在 API(使用 addEventListener)类中捕获它并再次分派,最后在我的活动。
因此,在 LoadExternal 和 API 中,这将类似于
在 myCampaign 中,
并且确保您需要在 APi 和 myCampaign 中添加侦听器。
PS 您应该避免使用 XMLDocument,它只是为了向后兼容 AS2,而应使用 XML。
需要 PPS addChild() 来处理显示列表对象 - 您不使用任何
Using callback is a rather straightforward solution, if you want to use event-driven approach you should inherit LoadExternal and API from EventDispatcher and dispatch your event, than you should catch it in API (with addEventListener) class and dispatch again and finally catch it in myCampaign.
So in LoadExternal and API this will look like
And in myCampaign it will be
And sure you will need to add listeners in APi and myCampaign.
P.S. You should avoid using XMLDocument it is made only for backward compatability with AS2, use XML instead.
P.P.S. addChild() is needed to work with display list objects - you are not working with any
我解决了问题!首先我放弃了 LoadExternal 类。
接下来,我将 API.as 中的 Loader(URLLoader) 设为 public,并在 myCampaign.as 中的 Loader 上添加了 EventListener,
因为我不知道这是否是正确的方法?有人可以证实这一点吗?
I solved the problem! First i discarded the LoadExternal class.
Next I made the Loader(URLLoader) in API.as public, and added EventListener on Loader in myCampaign.as
I don't know if this is the right approach? Can someone confirm this?