yozhik
yozhik

Reputation: 5084

Emulate server response in ActionScript 3

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?

Upvotes: 0

Views: 241

Answers (3)

user562566
user562566

Reputation:

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.

Upvotes: 3

Nathan Smith
Nathan Smith

Reputation: 37607

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));
        }

    }

Upvotes: 1

shanethehat
shanethehat

Reputation: 15580

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);
}

Upvotes: 2

Related Questions