Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.HTTPStatusEvent;
- import flash.events.IOErrorEvent;
- import flash.events.ProgressEvent;
- import flash.events.SecurityErrorEvent;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.net.URLRequestHeader;
- import flash.net.URLRequestMethod;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- [SWF(backgroundColor='#ffffff', frameRate='60')]
- public class Main extends Sprite {
- private var jsonLoader:URLLoader;
- private var tf:TextField;
- public function Main() {
- if (stage) init();
- else addEventListener(flash.events.Event.ADDED_TO_STAGE, init);
- }
- private function init(e:flash.events.Event = null):void {
- removeEventListener(flash.events.Event.ADDED_TO_STAGE, init);
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- var txtFormat:TextFormat = new TextFormat();
- txtFormat.color = 0x000000;
- txtFormat.size = 16;
- tf = new TextField();
- tf.wordWrap = true;
- tf.multiline = true;
- tf.selectable = true;
- tf.condenseWhite = true;
- tf.alwaysShowSelection = true;
- tf.autoSize = TextFieldAutoSize.LEFT;
- tf.text = "http://jsonplaceholder.typicode.com/users";
- tf.setTextFormat(txtFormat);
- tf.defaultTextFormat = txtFormat;
- tf.width = stage.stageWidth;
- tf.height = stage.stageHeight;
- addChild(tf);
- getJSON(tf.text);
- }
- //________________________________________________________ ROOT
- private function getJSON(str:String):void
- {
- if(jsonLoader) jsonLoader.close();
- jsonLoader = new URLLoader();
- jsonLoader.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
- jsonLoader.addEventListener(flash.events.Event.COMPLETE, apiLoadHandler);
- jsonLoader.addEventListener(flash.events.Event.OPEN, apiOpenHandler);
- jsonLoader.addEventListener(ProgressEvent.PROGRESS, apiLoadProgressHandler);
- jsonLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, apiSecurityErrorHandler);
- jsonLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, apiHttpStatusHandler);
- consoleLog("> Loading: " + str);
- var request:URLRequest=new URLRequest();
- request.url = str;
- request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];
- request.method = URLRequestMethod.GET;
- jsonLoader.load(request);
- }
- private function onIoError(event:IOErrorEvent):void
- {
- trace(event.toString());
- consoleLog("IOErrorEvent.IO_ERROR: " + event.toString());
- }
- private function apiOpenHandler(event:flash.events.Event):void
- {
- consoleLog("flash.events.Event.OPEN: " + event.toString());
- }
- private function apiLoadProgressHandler(event:ProgressEvent):void
- {
- consoleLog("ProgressEvent.PROGRESS: " + event.toString());
- }
- private function apiSecurityErrorHandler(event:SecurityErrorEvent):void
- {
- consoleLog("SecurityErrorEvent.SECURITY_ERROR: " + event.toString());
- }
- private function apiHttpStatusHandler(event:HTTPStatusEvent):void
- {
- consoleLog("HTTPStatusEvent.HTTP_STATUS: " + event.toString());
- }
- private function apiLoadHandler(event:flash.events.Event):void {
- var obj:* = JSON.parse(event.target.data);
- consoleLog("+ flash.events.Event.COMPLETE: " + obj.toString());
- }
- private function consoleLog(str:String):void
- {
- tf.text = str;
- }
- }
- }
Add Comment
Please, Sign In to add comment