HTTP Service:

Код:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
width="1000" height="500">
<mx:Script>
<![CDATA[
import mx.binding.utils.ChangeWatcher;
import mx.binding.utils.BindingUtils;
private var nameBinding:ChangeWatcher;
import fclass.fload.FHTTPService;
public var Fhttp:FHTTPService = new FHTTPService();
public function Init_Razdel():void {
this.nameBinding = BindingUtils.bindProperty(DG_Razdel,
"dataProvider", this.Fhttp, "FRezult_text");
this.Fhttp.HTTP_BEST();
}
]]>
</mx:Script>
<mx:DataGrid id="DG_Razdel" width="600" height="350" >
<mx:columns>
<mx:DataGridColumn dataField="number" headerText="№"/>
<mx:DataGridColumn dataField="name" headerText="Раздел"/>
</mx:columns>
</mx:DataGrid>
</mx:Canvas>
fclass/fload/FHTTPService.as;

Код:
package fclass.fload
{
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class FHTTPService
{
private var httpRequest:HTTPService = new HTTPService();
[Bindable] public var FRezult_text:ArrayCollection;
public function HTTP_BEST():void {
httpRequest.url="menu.xml?n="+Math.random();
httpRequest.send();
httpRequest.addEventListener(ResultEvent.RESULT,getResult);
}
private function getResult(event:ResultEvent):void {
var Arr:ArrayCollection = new ArrayCollection();
Arr = event.result.razdel.blok;
Arr.refresh();
//Имеем В результате ArrayCollection который подвязан под дата грид
FRezult_text = Arr;
}
}
}
Пример с URLLoader...

Код:
package fclass.fload
{
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;
public class FMURLLoader extends Sprite {
[Bindable] public var Info_forME:String;
[Bindable] public var FProgress_text:String;
[Bindable] public var FRezult_text:String;
public function MURLLoader():void {
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest("menu.xml?n="+Math.random());
try {
loader.load(request);
Info_forME = "Грузим файл"+"\n";
FProgress_text = "Загрузка ...";
} catch (error:Error) {
Info_forME = "Unable to load requested document."+"\n";
FProgress_text = "Ошибка при загрузке";
}
}
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
Info_forME += "Загрузился \n";
//На выходе имеем xml в представлении String что бы перевести в
//ArrayCollection надо не плохо повозиться..
FRezult_text = loader.data;
FProgress_text = "Файл загружен";
FFinish();
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function openHandler(event:Event):void {
Info_forME += "openHandler: " + event+"\n";
FProgress_text = "Загрузка ...";
}
private function progressHandler(event:ProgressEvent):void {
Info_forME += "progressHandler loaded: " + event.bytesLoaded + " total: " + event.bytesTotal+"\n";
//Отображаем процент загрузки!!! КРУТО
FProgress_text = "Загрузка: "+Math.round(100*event.bytesLoaded/event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
Info_forME += "securityErrorHandler: " + event+"\n";
FProgress_text = "Ошибка при загрузке";
FError();
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
Info_forME += "httpStatusHandler: " + event+"\n";
}
private function ioErrorHandler(event:IOErrorEvent):void {
Info_forME += "ioErrorHandler: " + event+"\n";
FProgress_text = "Ошибка при загрузке";
FError();
}
private function FError():void {
dispatchEvent(new Event("FLoadError", true));
}
private function FFinish():void {
dispatchEvent(new Event("FLoadFinish", true));
}
}
}