Created
November 11, 2011 16:54
-
-
Save AbraaoAlves/1358518 to your computer and use it in GitHub Desktop.
Class to work with RequestJson in actionScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.Sprite; | |
import flash.events.*; | |
import flash.net.*; | |
public class RequestJson extends Sprite { | |
private var loader:URLLoader; | |
private var request:URLRequest; | |
private var _completeHandler:Function; | |
public function setCompleteHandler(handler:Function):void | |
{ | |
_completeHandler = handler; | |
} | |
private var _httpStatusHandler:Function; | |
public function addHttpStatusHandler(handler:Function, status:Number):void | |
{ | |
_httpStatusHandler = handler; | |
} | |
public function RequestJson(url:String, params:*, method:String) | |
{ | |
loader = new URLLoader(); | |
request= new URLRequest(url); | |
request.requestHeaders["Content-Type"] = "aplication/json"; | |
request.requestHeaders["Accept"] = "aplication/json"; | |
request.method = method; | |
if(params) setParams(params); | |
configureListeners(loader); | |
} | |
public function Go():void | |
{ | |
try { | |
loader.load(request); | |
} catch (error:Error) { | |
trace(error); | |
} | |
} | |
private function setParams(params:*):void | |
{ | |
var variables : URLVariables = new URLVariables(); | |
for(var key in params) variables[key] = params[key] | |
request.data = variables; | |
} | |
private function configureListeners(dispatcher:IEventDispatcher):void | |
{ | |
dispatcher.addEventListener(Event.COMPLETE, completeHandler); | |
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); | |
} | |
private function completeHandler(event:Event):void | |
{ | |
_completeHandler(URLLoader(event.target).data); | |
} | |
private function httpStatusHandler(event:HTTPStatusEvent):void | |
{ | |
//TODO: inception(TODO: "colacar uma descricao mais simples que me lembre o que tenho de fazer") | |
trace(event); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment