這裏是如何從基於Adobe AIR應用程序服務器獲取數據。
<?xml version="1.0" encoding="utf-8"?>
<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">
<fx:Script>
<![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' && com.adobe.serialization.json.JSON.decode(urlLoader.data).length>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 & 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.
}
// ------------------------------------------------------------------------
]]>
</fx:Script>
<mx:DataGrid id="dg" top="100" width="600" height="600" horizontalCenter="0">
<mx:columns>
<mx:DataGridColumn width="200" dataField="name" headerText="Name"/>
<mx:DataGridColumn dataField="value" headerText="Value"/>
</mx:columns>
</mx:DataGrid>
<mx:Label top="15" color="#0D595A" fontSize="30" fontWeight="bold" horizontalCenter="4"
text="Get JSON Data From Server"/>
<mx:Button id="btn" top="70" width="200" label="Get JSON Data from Server"
chromeColor="#A7FEFF" fontWeight="bold" horizontalCenter="0"/>
</s:WindowedApplication>
現在,這是一個完整的代碼,讓您開始使用Adobe AIR App從服務器加載JSON數據。
在這個例子中,我使用的是import com.adobe.serialization.json.JSON;
。並且需要包含它以將以String格式接收的JSON數據解析/解碼爲本地ActionScript對象。您可以從GITHUB下載as3corelib
。
如果您使用Adobe Flash創建AIR App,則必須將此庫包含在庫/源路徑中。
如果您使用的Adobe Flash Builder的4.6高級因爲我是,那麼:
1)在Adobe Flash Builder中確保你在Flash Builder角度,從Package Explorer中,右鍵點擊您的項目並選擇屬性。 (這將打開您的項目屬性)
2)在項目的屬性對話框中,選擇「Flex構建路徑」。
3)在「Flex Build Path」對話框中,選擇Tab:「Library path」。
4)單擊右側的「添加SWC」按鈕。並瀏覽到「as3corelib.swc」文件的位置。 (如果你把在「庫」文件夾/在你的應用程序目錄主目錄,其中「濱調試」目錄也位於該文件比較好。)現在,選擇文件並單擊確定按鈕。
5)再次單擊確定按鈕關閉項目的屬性對話框。
6)現在保存您的所有項目文件和所有內容,然後關閉Flash Builder。
7)現在重新啓動Flash Builder。運行或調試您的項目。您應該能夠在控制檯日誌中看到我們的trace
語句的輸出。
8)如果編譯項目時出現錯誤,則還必須包括com.adobe.serialization.json.JSON;
包項目的「SRC」目錄下,通過解壓縮,包括對源ActionScript類Zip文件as3corelib。
9)現在它應該工作。再次重新啓動Flash Builder。現在,您的項目SRC目錄下應該有一個COM目錄,其中包括其他目錄和的as3corelib包的ActionScript類。 (至少我現在沒有任何問題,我已經添加as3corelib.swc和包的源文件)
下載「的as3corelib」這裏github。它包含SRC目錄as3corelib.swc文件中的lib目錄和ActionScript 3.0源類com.adobe.serialization.json
包。 (您可能需要將COM目錄中的所有內容複製到庫目錄中的項目的根目錄。這樣就可以調用它的解碼和編碼在你的代碼方法和意志被編譯爲您的SWF和AIR文件。)
我不認爲您可以在Adobe AIR中使用JavaScript/jQuery。 它必須是您正在開發的基於Adobe AIR的應用程序。 請將您的Flex/Flash Builder代碼發佈到AIR應用程序。 –
您可以在基於AIR的應用程序中從服務器獲取數據,而無需使用JavaScript或jQuery。 我會發布我的示例代碼,以獲取來自服務器的動作腳本3.0中的數據。 –