Skip to content

Instantly share code, notes, and snippets.

@AbraaoAlves
Created November 11, 2011 16:54
Show Gist options
  • Save AbraaoAlves/1358518 to your computer and use it in GitHub Desktop.
Save AbraaoAlves/1358518 to your computer and use it in GitHub Desktop.
Class to work with RequestJson in actionScript
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