1

I am new to action script 3.0. Help me to understand how can I load data with URLLoader.

So, I have an aplication like:

var PATH:String = "http://www.somesite.com/myFirstPage.php?a=10&b=20";


var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);

function urlLoader_complete(evt:Event):void {
    some_result = urlLoader.data;
}

php script looks like:

<?php
//retrieve GET params

int c = a + b; //a and b come from GET request

return "{{"result_equal":"20"}}"; //result in JSON

?>

I don't really understand how JSON result from .php page gets in my URLLoader object. Help me by simple example, please. Thanx!

yozhik
  • 4,644
  • 14
  • 65
  • 98

4 Answers4

2

You will want to use this project: https://github.com/mikechambers/as3corelib

Example usage:

<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;
        import com.adobe.serialization.json.JSON;

        private function onJSONLoad(event:ResultEvent):void
        {
            var rawData:String = String(event.result);
            var arr:Array = (JSON.decode(rawData) as Array);

            var dp:ArrayCollection = new ArrayCollection(arr);

            grid.dataProvider = dp;
        }
    ]]>
</mx:Script>

<mx:HTTPService
    id="service"
    resultFormat="text"
    url="http://weblogs.macromedia.com/mesh/mashedpotato.json"
    result="onJSONLoad(event)" />
Joe
  • 80,724
  • 18
  • 127
  • 145
2

You need a few things here. First off, you'll need a JSON library, because somehow it isn't built into Flash, even with their modified E4X core:

https://github.com/mikechambers/as3corelib

This should give you the following bits of code:

import com.adobe.serialization.json.JSON;

function urlLoader_complete(e:Event):void 
{
   var loader:URLLoader = URLLoader(e.target);
   var some_result:Object = JSON.decode(loader.data);
}

Now... your PHP code is a mess. The best way to create JSON is to just use the json_encode function:

$retVal = array('result_equal'=>20);
echo json_encode($retVal);
John Green
  • 13,241
  • 3
  • 29
  • 51
  • Please, can you post all PHP code example I need? So I got parameters, make something with them, and JUST PRINT THEM WITH ECHO OPERATOR??? Thats all? And URLLoader download it by itself?? – yozhik Aug 04 '11 at 22:08
  • `$a = $_GET['a']; $b = $_GET['b']; $c = $a + $b; $result = array('result_equal' => $c); echo json_encode($result);` – Dawid Sajdak Aug 05 '11 at 08:27
  • Yozhik: Yup. : ) Dawid's code is pretty close, although I'd recommend specifically casting your $_GET['a'], etc to ints: $c = intval($_GET['a'],10) + intval($_GET['b'],10); $retVal = array('result_equal'=>$c);echo $retVal; – John Green Aug 05 '11 at 12:36
1

PHP code should looks like this:

$result = array("result_equal" => 20);
return json_encode($result);
Dawid Sajdak
  • 3,064
  • 2
  • 23
  • 37
0

So, this is what I get: Please, say whats wrong, what good! Thanx1

package  
{
    import data.*;
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.*;
    import flash.net.*;

    public class UserInfoProxy 
    {
        public static const GET_USER_INFO:DataCallConfigVO = new DataCallConfigVO( "COMMON", 'GET_USER_INFO', true, null, null);

        public function UserInfoProxy() 
        {               
            trace("Start request");
            var request:URLRequest = new URLRequest("http://www.avrora.com.localhost/myservlet.php");
            var loader:URLLoader = new URLLoader();

            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            loader.load(request);
        }


        private function completeHandler(event:Event):void 
        {
            var loader:URLLoader = URLLoader(event.target);
            trace("Complete");
            trace("completeHandler: " + loader.data);
        }

        private function progressHandler(event:ProgressEvent):void 
        {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }


        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void
        {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
        {
            trace("ioErrorHandler: " + event);
        }
    }

}

And this is my PHP code, that must return JSON data:

 <? 

//here I can process all input GET, POST params, database etc...
    $someJSONresponse = "{'username':'yozhik', 'age':'18'}";

    //here just echo result and it will be automatically downloaded??
    echo $someJSONresponse;

    ?>
yozhik
  • 4,644
  • 14
  • 65
  • 98