728x90

- HTTP 프로토콜을 사용해 특정 URL을 호출하여 XML 데이터를 전달 받을 수 있는 방식으로 WAS를 구성하지

않아도 되며 쉽게 사용할 수 있다.

- mx.rpc.http.mxml.HTTPService 패키지에 포함.

- HTTPService 인스턴스.send() 메소드를 사용해 HTTPService를 호출한다.

- HTTPService 인스턴스.lastResult 속성으로 XML 결과값을 참조할 수 있다.

- event.result 이벤트를 사용해도 XML 결과값을 참조할 수 있다.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete=
"MyHTTPService.send()"> <!-- HTTPService 호출 -->
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
<mx:
HTTPService id="MyHTTPService" useProxy="false"
url="http://chocodonut.tistory.com/rss"
result="Alert.show('HTTPService 호출 성공: ' +

event.result.rss.channel.title)"
fault="Alert.show('HTTPService 호출 실패: ' + event.fault.message)"/>

<mx:Panel x=
"10" y="10" width="600" height="600" layout="absolute">
<mx:DataGrid id=
"DG" x="10" y="10" width="560"
dataProvider="{MyHTTPService.lastResult.rss.channel.item}"
itemClick=
"TA.htmlText=DG.selectedItem.description">
<mx:columns>
<mx:DataGridColumn headerText=
"Title" dataField="title"/>
<mx:DataGridColumn headerText=
"Date" dataField="pubDate"/>
<mx:DataGridColumn headerText=
"Category" dataField="category"/>
</mx:columns>
</mx:DataGrid>
<mx:TextArea id=
"TA" x="10" y="160" width="560" height="350"/>
</mx:Panel>
</mx:Application>

- crossdomain.xml 파일: 다른 도메인으로부터의 접속을 허용할지 설정한다.

<?xml version="1.0"?>
<cross-domain-policy>
<
allow-access-from domain="*"/>
</cross-domain-policy>

- JSP 페이지를 호출해서 XML 데이터를 가져오는 예제.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete=
"MyHTTPService.send()">

<mx:HTTPService id="MyHTTPService" useProxy="false"

url="http://chocodonut.tistory.com/GetXmlData.jsp"/>

<mx:Panel x=
"10" y="10" width="600" height="600" layout="absolute">
<mx:DataGrid id=
"DG" x="10" y="10" width="560"
dataProvider="{MyHTTPService.lastResult.result_set.record}">

<!-- result_set과 record는 JSP 페이지가 생성하는 XML 요소들 -->
<mx:columns>
<mx:DataGridColumn headerText=
"ID" dataField="id"/>
<mx:DataGridColumn headerText=
"Name" dataField="name"/>
<mx:DataGridColumn headerText=
"Age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:Application>

- JSP 페이지에 매개변수를 전달하는 예제.

- request 태그 내에 서버로 전달할 데이터를 지정한다.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" layout="absolute">
<mx:
HTTPService id="MyHTTPService" useProxy="false"
url=
"http://chocodonut.tistory.com/GetXMLData.jsp">
<mx:
request> <!-- 전달하는 매개변수 -->

<id>{CB.selectedItem}</id>

</mx:request>
</mx:HTTPService>

<mx:Panel x=
"10" y="10" width="600" height="600" layout="absolute">
<mx:DataGrid id=
"DG" x="10" y="10" width="560"
dataProvider="{MyHTTPService.lastResult.result_set.record}">
<mx:columns>
<mx:DataGridColumn headerText=
"ID" dataField="id"/>
<mx:DataGridColumn headerText=
"Name" dataField="name"/>
<mx:DataGridColumn headerText=
"Age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:ComboBox id=
"CB" change="MyHTTPService.send()" x="10" y="160">

<!-- HTTPService 호출 -->

<mx:ArrayCollection>
<mx:String>0</mx:String>
<mx:String>1</mx:String>
<mx:String>2</mx:String>
<mx:String>3</mx:String>
<mx:String>4</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
</mx:Panel>
</mx:Application>

- ActionScript를 이용한 예제.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=
"http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.http.mxml.HTTPService;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;

private function f():void {
var
MyHTTPService:HTTPService = new HTTPService();
MyHTTPService.url =
"http://chocodonut.tistory.com/GetXMLData.jsp"
MyHTTPService.addEventListener(ResultEvent.RESULT, resultHandler);
MyHTTPService.addEventListener(FaultEvent.FAULT, faultHandler);

var
obj:Object = new Object();
obj.id =
CB.selectedItem;

MyHTTPService.send(obj); // HTTPService를 호출하면서 매개변수 전달.
}

private function resultHandler(event:ResultEvent):void {
DG.dataProvider = event.result.result_set.record;
}

private function faultHandler(event:FaultEvent):void {
Alert.show(
event.fault.message);
}
]]>
</mx:Script>

<mx:Panel x=
"10" y="10" width="600" height="600" layout="absolute">
<mx:DataGrid id=
"DG" x="10" y="10" width="560">
<mx:columns>
<mx:DataGridColumn headerText=
"ID" dataField="id"/>
<mx:DataGridColumn headerText=
"Name" dataField="name"/>
<mx:DataGridColumn headerText=
"Age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
<mx:ComboBox id=
"CB" change="f()" x="10" y="160">
<mx:ArrayCollection>
<mx:String>0</mx:String>
<mx:String>1</mx:String>
<mx:String>2</mx:String>
<mx:String>3</mx:String>
<mx:String>4</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
</mx:Panel>
</mx:Application>

728x90

'Flex' 카테고리의 다른 글

flexlib  (0) 2012.07.29
Flex String 줄바꿈  (0) 2012.07.29
오라클 날짜, 시간으로 조회  (0) 2012.07.29
swf파일 '개체가 필요합니다' 에러  (0) 2012.07.29
플렉스 스타일 미리보기  (0) 2012.07.29

+ Recent posts