Actionscript 3.0,从外部类获取xml?

发布于 2024-10-18 01:21:17 字数 2386 浏览 1 评论 0原文

我正在尝试调用返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戏剧牡丹亭 2024-10-25 01:21:17

您需要将 _loaded 变量设为公共(public var returned:XMLDocument),然后您就可以这样做,

public function GetMakes() : XMLDocument
{       
   return _xmlDocument.loaded;
}

尽管您可能想为变量使用其他名称...

You need to make the _loaded variable public (public var loaded:XMLDocument) and then you can do

public function GetMakes() : XMLDocument
{       
   return _xmlDocument.loaded;
}

although you might want to use other names for your variables...

走野 2024-10-25 01:21:17

您必须向加载程序提供回调例程,以便在收到数据后进行调用。

像这样的东西
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});

稳稳的幸福 2024-10-25 01:21:17

使用回调是一个相当简单的解决方案,如果您想使用事件驱动的方法,您应该从 EventDispatcher 继承 LoadExternal 和 API 并分派您的事件,而不是您应该在 API(使用 addEventListener)类中捕获它并再次分派,最后在我的活动。

因此,在 LoadExternal 和 API 中,这将类似于

public function onComplete(e:Event):void  
{  
   dispatchEvent(e);
}

在 myCampaign 中,

public function onComplete(e:Event):void  
{  
   _loaded = new XMLDocument(e.target.data);
   trace(_loaded); 
}

并且确保您需要在 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

public function onComplete(e:Event):void  
{  
   dispatchEvent(e);
}

And in myCampaign it will be

public function onComplete(e:Event):void  
{  
   _loaded = new XMLDocument(e.target.data);
   trace(_loaded); 
}

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

奢华的一滴泪 2024-10-25 01:21:17

我解决了问题!首先我放弃了 LoadExternal 类。
接下来,我将 API.as 中的 Loader(URLLoader) 设为 public,并在 myCampaign.as 中的 Loader 上添加了 EventListener,

package references.campaign
{
    import flash.events.Event;
    import references.utils.*;
    import flash.display.*;
    import flash.net.*;


    public class myCampaign extends MovieClip
    {       
        public function myCampaign()
        {
            var api = new API();
            api.InvokeMethod("MyMethod");
            api.Loader.addEventListener(Event.COMPLETE, MethodLoaded);
        }

        function MethodLoaded(e:Event)
        {
            var array:Array = new Array();          
            var xml:XML = new XML(e.target.data);
            trace(xml);
        }
    }
}

package references.utils
{
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLLoaderDataFormat;

    public class API
    {   
        const ApiKey:String = "ApiKEY=random_gen_key";
        const URL:String = "http://api.myURL.com/";
        public var Loader:URLLoader;

        public function API()
        {   

        }

        public function InvokeMethod(method:String) : void  
        {       
            Loader = new URLLoader();
            Loader.dataFormat = URLLoaderDataFormat.TEXT;
            Loader.load(new URLRequest(URL + method + "?" + ApiKey));
        }
    }
}

因为我不知道这是否是正确的方法?有人可以证实这一点吗?

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

package references.campaign
{
    import flash.events.Event;
    import references.utils.*;
    import flash.display.*;
    import flash.net.*;


    public class myCampaign extends MovieClip
    {       
        public function myCampaign()
        {
            var api = new API();
            api.InvokeMethod("MyMethod");
            api.Loader.addEventListener(Event.COMPLETE, MethodLoaded);
        }

        function MethodLoaded(e:Event)
        {
            var array:Array = new Array();          
            var xml:XML = new XML(e.target.data);
            trace(xml);
        }
    }
}

package references.utils
{
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLLoaderDataFormat;

    public class API
    {   
        const ApiKey:String = "ApiKEY=random_gen_key";
        const URL:String = "http://api.myURL.com/";
        public var Loader:URLLoader;

        public function API()
        {   

        }

        public function InvokeMethod(method:String) : void  
        {       
            Loader = new URLLoader();
            Loader.dataFormat = URLLoaderDataFormat.TEXT;
            Loader.load(new URLRequest(URL + method + "?" + ApiKey));
        }
    }
}

I don't know if this is the right approach? Can someone confirm this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文