Flex request webservice

Source: Internet
Author: User

In addition to being able to integrate with spring, flex sometimes uses flex to access webservice. flex provides a set of methods for accessing webservice, which is also very simple. My learning achievements will be shared below

1. Search for the webservice Service for testing

Http://www.webxml.com.cn/zh_cn/index.aspx

This website provides many WebServices for us to learn and use.

2. Start to use flex to request webservice. Here we use the number attribution location and weather forecast as an example.

(1) load the wsdl using the flex tag

<s:WebService wsdl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl" id="webservice"><s:operation name="getMobileCodeInfo" result="getMessage(event)" fault="onFault(event)"/></s:WebService><s:WebService wsdl="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl" id="webservice2" ><s:operation name="getWeather" result="getWeatherResult(event)" resultFormat="object" fault="onFault(event)"/></s:WebService>

Wsdl is the wsdl address of the web service. The id must be written for later use. The name in operation is the method exposed by webservice, and the result is the callback function, fault indicates the function call after an error occurs. If this parameter is not configured, it is OK.

(2) call the home location service of the mobile phone number

webservice.getOperation("getMobileCodeInfo").send(number.text);

Webservice is the id of the first tag, indicating that we want to use the service in it. The getOperation parameter is the name of the method to be called, and the send parameter is the parameter we want to provide to the server.

(3) Use the callback function to process webservice returned results

var ss:String=event.result as String;Alert.show(ss);

The weather forecast service returns a one-dimensional array.

The weather forecast result is as follows:


The following is all the code:

<? Xml version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: fx = "http://ns.adobe.com/mxml/2009" xmlns: s = "library: // ns.adobe.com/flex/spark" xmlns: mx = "library: // ns.adobe.com/flex/mx "minWidth =" 955 "minHeight =" 600 "> <fx: Declarations> <! -- Place non-visual elements (e.g., services, value objects) here --> <s: WebService wsdl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx? Wsdl "id =" webservice "> <s: operation name =" getMobileCodeInfo "result =" getMessage (event) "fault =" onFault (event) "/> </s: webService> <s: WebService wsdl = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx? Wsdl "id =" webservice2 "> <s: operation name =" getWeather "result =" getWeatherResult (event) "resultFormat =" object "fault =" onFault (event) "/> </s: WebService> </fx: Declarations> <fx: Script> <! [CDATA [import mx. collections. arrayCollection; import mx. controls. alert; import mx. rpc. events. resultEvent; import mx. events. closeEvent; public function getMessage (event: ResultEvent) {var ss: String = event. result as String; Alert. show (ss);} public function getWeatherResult (event2: ResultEvent): void {var ss2: ArrayCollection = ArrayCollection (event2.result); Alert. show (ss2 [1] + "\ n" + ss2 [4] + "\ n" + ss2 [5]);} public functio N invokeWebservice (event: Event) {webservice. getOperation ("getMobileCodeInfo "). send (number. text);} public function invokeWebservice2 (event: Event) {webservice2.getOperation ("getWeather "). send (city. text, '');} public function onFault (event: Event): void {Alert. show ("Access Error");}/* public function show (event: Event): void {Alert. yesLabel = 'yes'; Alert. noLabel = 'no'; Alert. cancelLabel = 'cancel'; Alert. show ('are the operations correct? ', 'Are you sure? ', 1 | 2 | 8, this, back);} public function back (event: CloseEvent): void {if (event. detail = Alert. YES) {Alert. show ("you have selected yes");} else if (event. detail = Alert. NO) {Alert. show ("You selected no");} else {Alert. show ("you have chosen to cancel") ;}} */]> </fx: Script> <s: VGroup> <s: HGroup paddingTop = "30" verticalAlign = "middle"> <s: Label text = "Enter your phone number"/> <s: textInput id = "number"/> <s: Button label = "" click = "invokeWebservice (event)"/> </s: HGroup> <s: H Group paddingTop = "30" verticalAlign = "middle"> <s: Label text = "Enter city name"/> <s: TextInput id = "city"/> <s: button label = "get weather" click = "invokeWebservice2 (event)"/> </s: HGroup> <! -- <S: HGroup paddingTop = "30" verticalAlign = "middle"> <s: Button label = "show confirmation box" click = "show (event) "/> </s: HGroup> --> </s: VGroup> </s: Application>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.