Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how to get Data from Server in Adobe AIR based applications.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()" width="620" height="740" frameRate="30"&gt; &lt;fx:Script&gt; &lt;![CDATA[ import com.adobe.serialization.json.JSON; // var ini // ------------------------------------------------------------------------ private var filePath:String = "http://www.mysite.com/test.php?callback=?";// URL or path to JSON Source // can be "assest/jsondata.txt"; private var urlLoader:URLLoader; private var jsonDataArray:Array; private var jsonObj:Object; private var request:URLRequest; private var variables:URLVariables; private var requestFilters:String; private var requestHeaders:Array;//used for content-type headers and such private var arr:Array = new Array();// saving all objects to dataGrid // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ private function init():void { requestHeaders = new Array(new URLRequestHeader("content-type","application/json"));//add as many comma separated headers as you need to send to server // Add event listener for button click btn.addEventListener(MouseEvent.CLICK,loadJSONData); } // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ private function loadJSONData(e:MouseEvent=null):void { // Load file urlLoader = new URLLoader(); urlLoader.addEventListener(Event.COMPLETE, fileLoaded,false,0,true); urlLoader.addEventListener(IOErrorEvent.IO_ERROR, fileLoadFailed); request = new URLRequest(); request.method = URLRequestMethod.POST;//use URLRequestMethod.GET if you want request.requestHeaders = requestHeaders;//headers to send request.data = 'jsonCallback';//can send data/parameters with this request to server. If server requires data/parameters sent as JSON string, you can accomplish it passing JSON String like this: request.data = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": { "filter" : {} }, "id": 1}'; request.url = filePath; urlLoader.load(request); } // ------------------------------------------------------------------------ private function fileLoadFailed(e:Event):void { trace("Failed: "+e.target.data.toString()); } // ------------------------------------------------------------------------ private function fileLoaded(e:Event):void { // Clean up file load event listeners urlLoader.removeEventListener(Event.COMPLETE, fileLoaded); // If you wanted to get the data from the event use the line below //var urlLoader:URLLoader = URLLoader(event.target); trace("urlLoader.data: "+urlLoader.data+", Data type: "+typeof(urlLoader.data)); var Obj:Object = urlLoader.data; // Parse the response to get Array or Object depending on how the JSON is formatted on Server. //jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data); // converts urlLoader.data to a localized Object or Array // check if the returned data is an Object if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object') jsonObj = com.adobe.serialization.json.JSON.decode(urlLoader.data); // if data returned is an Array else if(typeof(com.adobe.serialization.json.JSON.decode(urlLoader.data))=='object' &amp;&amp; com.adobe.serialization.json.JSON.decode(urlLoader.data).length&gt;2) jsonDataArray = com.adobe.serialization.json.JSON.decode(urlLoader.data); /* Your Data Format { "sites": [ { "siteName": "JQUERY4U", "domainName": "http://www.jquery4u.com", "description": "#1 jQuery Blog for your Daily News, Plugins, Tuts/Tips &amp;amp; Code Snippets." }, { "siteName": "BLOGOOLA", "domainName": "http://www.blogoola.com", "description": "Expose your blog to millions and increase your audience." }, { "siteName": "PHPSCRIPTS4U", "domainName": "http://www.phpscripts4u.com", "description": "The Blog of Enthusiastic PHP Scripters" } ] } */ // now you should be able to access the data like this: trace(jsonObj.sites[0].domainName); // or trace(jsonDataArray[0].siteName); // Now you can loop through your received data as usual. } // ------------------------------------------------------------------------ ]]&gt; &lt;/fx:Script&gt; &lt;mx:DataGrid id="dg" top="100" width="600" height="600" horizontalCenter="0"&gt; &lt;mx:columns&gt; &lt;mx:DataGridColumn width="200" dataField="name" headerText="Name"/&gt; &lt;mx:DataGridColumn dataField="value" headerText="Value"/&gt; &lt;/mx:columns&gt; &lt;/mx:DataGrid&gt; &lt;mx:Label top="15" color="#0D595A" fontSize="30" fontWeight="bold" horizontalCenter="4" text="Get JSON Data From Server"/&gt; &lt;mx:Button id="btn" top="70" width="200" label="Get JSON Data from Server" chromeColor="#A7FEFF" fontWeight="bold" horizontalCenter="0"/&gt; &lt;/s:WindowedApplication&gt; </code></pre> <p>Now this one is the complete code to get you started with Adobe AIR App that loads JSON Data from Server.</p> <p>I'm using <code>import com.adobe.serialization.json.JSON;</code> in this example. And it needs to be included to parse/decode JSON Data received in String format to Local ActionScript Object.</p> <p>You can Download <code>as3corelib</code> from GITHUB.</p> <p><strong>If you are using Adobe Flash to create your AIR App</strong>, then you must include this library in your Libraries/Source paths.</p> <p><strong>If you're using Adobe Flash Builder 4.6 Premium</strong> as am I, then:</p> <p><strong>1)</strong> In Adobe Flash Builder make sure you're in Flash Builder Perspective, from Package Explorer, Right-Click your Project and Select Properties. (This will Open up your Projects Properties)</p> <p><strong>2)</strong> In the Properties Dialogue of your Project, Select "Flex Build Path".</p> <p><strong>3)</strong> In "Flex Build Path" Dialogue, Select the Tab: "Library path".</p> <p><strong>4)</strong> Click "Add SWC" button on the Right side. And browse to the location of: "<em><strong>as3corelib.swc</strong></em>" file. (better if you place this file in "<em><strong>libs</strong></em>" folder/directory in your Applications main directory, where "<em><strong>bin-debug</strong></em>" directory also resides.) Now Select the file and click OK button.</p> <p><strong>5)</strong> Again click OK button to close the Project's Properties Dialogue Box.</p> <p><strong>6)</strong> Now save your all project files and everything, and close Flash Builder.</p> <p><strong>7)</strong> Now Restart Flash Builder. Run or Debug your Project. You should be able to see output of our <code>trace</code> statements in Console Log.</p> <p><strong>8)</strong> If there are Errors when compiling your Project, then you must also include the <code>com.adobe.serialization.json.JSON;</code> package in your Project's "<em><strong>src</strong></em>" directory, by uncompressing the Zip file that includes the Source ActionScript Classes for the <strong>as3corelib</strong>.</p> <p><strong>9)</strong> Now it should work. Better restart Flash Builder again. Now your Project's <em><strong>SRC</strong></em> directory should have a <em><strong>com</strong></em> directory which includes other Directories and ActionScript Classes of the <strong>as3corelib</strong> package. (At least I have no problem now that I have added <em><strong>as3corelib.swc</strong></em> and the source files of the package)</p> <p>Download the "<strong>as3corelib</strong>" here on <strong><a href="https://github.com/mikechambers/as3corelib/downloads" rel="nofollow">github</a></strong>. It contains the <strong>as3corelib.swc</strong> file in <em><strong>lib</strong></em> directory and ActionScript 3.0 source classes of <code>com.adobe.serialization.json</code> package in <em><strong>src</strong></em> directory. (You might need to copy the <em><strong>com</strong></em> directory with all contents to <em><strong>libs</strong></em> directory in your Project's <em>root</em> directory. So that You can call its <em>decode</em> and <em>encode</em> methods in your code and will be compiled in to your <strong>SWF</strong> and <strong>AIR</strong> files.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload