home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / com / facebook / air / JSONCall.as next >
Encoding:
Text File  |  2010-06-23  |  3.2 KB  |  101 lines

  1. package com.facebook.air
  2. {
  3.    import com.adobe.crypto.MD5;
  4.    import com.adobe.serialization.json.JSON;
  5.    import flash.events.Event;
  6.    import flash.events.EventDispatcher;
  7.    import flash.events.IOErrorEvent;
  8.    import flash.events.SecurityErrorEvent;
  9.    import flash.net.URLLoader;
  10.    import flash.net.URLLoaderDataFormat;
  11.    import flash.net.URLRequest;
  12.    import flash.net.URLRequestMethod;
  13.    import flash.net.URLVariables;
  14.    
  15.    public class JSONCall extends EventDispatcher
  16.    {
  17.       protected var sessionData:SessionData;
  18.       
  19.       protected var apiKey:String;
  20.       
  21.       protected var req:URLRequest;
  22.       
  23.       protected var loader:URLLoader;
  24.       
  25.       public function JSONCall(param1:SessionData, param2:String)
  26.       {
  27.          super();
  28.          this.sessionData = param1;
  29.          this.apiKey = param2;
  30.          this.req = new URLRequest("http://api.facebook.com/restserver.php");
  31.          this.req.contentType = "application/x-www-form-urlencoded";
  32.          this.req.method = URLRequestMethod.GET;
  33.          this.loader = new URLLoader();
  34.          this.loader.dataFormat = URLLoaderDataFormat.TEXT;
  35.          this.loader.addEventListener(Event.COMPLETE,this.onComplete);
  36.          this.loader.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
  37.          this.loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onSecurityError);
  38.       }
  39.       
  40.       protected function onIOError(param1:IOErrorEvent) : void
  41.       {
  42.          dispatchEvent(param1);
  43.       }
  44.       
  45.       protected function onSecurityError(param1:SecurityErrorEvent) : void
  46.       {
  47.          dispatchEvent(param1);
  48.       }
  49.       
  50.       protected function onComplete(param1:Event) : void
  51.       {
  52.          var _loc3_:* = undefined;
  53.          var _loc2_:String = this.loader.data;
  54.          if(_loc2_.indexOf("<") != 0)
  55.          {
  56.             _loc3_ = com.adobe.serialization.json.JSON.decode(_loc2_);
  57.             if(_loc3_.constructor == Object && Boolean(_loc3_.error_code))
  58.             {
  59.                dispatchEvent(new JSONEvent(JSONEvent.FAILURE,_loc3_));
  60.             }
  61.             else
  62.             {
  63.                dispatchEvent(new JSONEvent(JSONEvent.SUCCESS,_loc3_));
  64.             }
  65.          }
  66.          else
  67.          {
  68.             dispatchEvent(new JSONEvent(JSONEvent.FAILURE));
  69.          }
  70.       }
  71.       
  72.       public function call(param1:String, param2:Object = null) : void
  73.       {
  74.          var _loc4_:String = null;
  75.          var _loc5_:Array = null;
  76.          var _loc6_:String = null;
  77.          var _loc3_:URLVariables = new URLVariables();
  78.          for(_loc4_ in param2)
  79.          {
  80.             _loc3_[_loc4_] = param2[_loc4_];
  81.          }
  82.          _loc3_.v = "1.0";
  83.          _loc3_.format = "JSON";
  84.          _loc3_.method = param1;
  85.          _loc3_.api_key = this.apiKey;
  86.          _loc3_.call_id = new Date().time.toString();
  87.          _loc3_.session_key = this.sessionData.session_key;
  88.          _loc5_ = new Array();
  89.          for(_loc6_ in _loc3_)
  90.          {
  91.             _loc5_.push(_loc6_ + "=" + _loc3_[_loc6_]);
  92.          }
  93.          _loc5_.sort();
  94.          _loc3_.sig = MD5.hash(_loc5_.join("") + this.sessionData.secret);
  95.          this.req.data = _loc3_;
  96.          this.loader.load(this.req);
  97.       }
  98.    }
  99. }
  100.  
  101.