在 ActionScript 3 中模拟服务器响应

发布于 2024-11-24 07:49:34 字数 310 浏览 6 评论 0原文

我有一个用 ActionScript 3 制作的客户端应用程序。还有一个用 Java 制作的服务器。服务器将以 JSON 格式发送响应,但尚未完成。因此,我需要以某种方式在某个文件中模拟来自服务器的硬编码响应。获取某些密钥的响应。

例如:

request: http://www.serverscript.com/GET_INFO?a=2&e="hello"

response: {some JSON object}

在iPhone中,我们有可以使用的pList文件。
我可以在这里使用什么以及如何使用?

I have a client app made in ActionScript 3. And a server on Java. The server will send responses in JSON format, but it is not already done. So, I need to emulate somehow, hardcoded responses from server in some file. To get the responses for some key.

For example:

request: http://www.serverscript.com/GET_INFO?a=2&e="hello"

response: {some JSON object}

In the iPhone, we have the pList files that we can use.
What can I use here and how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

冬天旳寂寞 2024-12-01 07:49:34

简单的解决方案:在计算机上运行服务器:

http://www.apachefriends.org/en/xampp .html

这样你就不必模拟任何东西。您可以将服务器端代码和客户端代码放在同一台计算机上,完成后无需在任何地方进行重构,只需上传即可完成。您可以编写一个快速的 php 脚本来输出静态 json 对象,这将在完成时模拟与您的 java 服务的连接。

Easy solution: Run a server on your computer:

http://www.apachefriends.org/en/xampp.html

That way you don't have to simulate anything. You can have your server side code and your client side code on the same machine, no refactoring required anywhere when you're done just upload and you're done. You can write a quick php script to output a static json object and this will simulate connecting with your java service when it's completed.

千里故人稀 2024-12-01 07:49:34

您可以从包含虚拟 JSON 的纯文本文件加载:

var file:URLRequest = new URLRequest('dummy.txt');
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,onTextLoaded);
loader.load(file);

function onTextLoaded(evt:Event):void
{
    //trace the loaded content
    trace(URLLoader(evt.target).data);
    //decode the JSON
    var useableObject:Object = JSON.decode(URLLoader(evt.target).data);
}

You can load in from a plain text file containing your dummy JSON:

var file:URLRequest = new URLRequest('dummy.txt');
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,onTextLoaded);
loader.load(file);

function onTextLoaded(evt:Event):void
{
    //trace the loaded content
    trace(URLLoader(evt.target).data);
    //decode the JSON
    var useableObject:Object = JSON.decode(URLLoader(evt.target).data);
}
伏妖词 2024-12-01 07:49:34

您确实希望将与服务器之间的通信包装在自己的类中,这样您就可以在准备就绪时将服务器代码重构到其中,例如

    public class ServerGateway extends EventDispatcher
    {
        public static const SERVER_RESPONSE_EVENT:String = "serverResponseEvent";
        public var responseData:String = "";

        public function getInfo():void 
        {
            //load you file here for now but replace with server calls when its done
            var url:String = "your file path";
            var myLoader:URLLoader = new URLLoader();           
            myLoader.addEventListener(Event.COMPLETE, handleServerGetInfo);
            var request:URLRequest = new URLRequest("file.txt");
            myLoader.load(request);         
        }

        private function handleServerGetInfo(event:Event):void 
        {
            // this will need replacing to handle server responses when done
            var myLoader:URLLoader = event.target as URLLoader;
            myLoader.removeEventListener(Event.COMPLETE, handleServerGetInfo);
            responseData = myLoader.data as String;

            dispatchEvent(new Event(SERVER_RESPONSE_EVENT));
        }

    }

You really want to wrap communication to and from the server in its own class, that way you can refactor the server code into it when its ready e.g.

    public class ServerGateway extends EventDispatcher
    {
        public static const SERVER_RESPONSE_EVENT:String = "serverResponseEvent";
        public var responseData:String = "";

        public function getInfo():void 
        {
            //load you file here for now but replace with server calls when its done
            var url:String = "your file path";
            var myLoader:URLLoader = new URLLoader();           
            myLoader.addEventListener(Event.COMPLETE, handleServerGetInfo);
            var request:URLRequest = new URLRequest("file.txt");
            myLoader.load(request);         
        }

        private function handleServerGetInfo(event:Event):void 
        {
            // this will need replacing to handle server responses when done
            var myLoader:URLLoader = event.target as URLLoader;
            myLoader.removeEventListener(Event.COMPLETE, handleServerGetInfo);
            responseData = myLoader.data as String;

            dispatchEvent(new Event(SERVER_RESPONSE_EVENT));
        }

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