I have been trying to learn the Adobe Flex framework and Flex Builder. In order to use flex, you don't need Flex Builder, but it is a nice application built on top of the Eclipse IDE. It comes in the standalone version, or it can be a plug-in to Eclipse. Either way it is Eclipse re-purposed for writing flex and actionscript. I wanted to see how easily it was to connect to a webservice, so I found a nicely documented webservice to connect my application to. I am using the ESV Bible Web Service. While it took me quite a while to get a handle on how to write code using flex, once I got the hang of it, it is rather remarkable how few lines of code you actually need to build the application. This application, which compiles out to be a .swf file is only 41 lines of flex. I'll post it below.
Try it out!
It requires that you have Flash Player 9 installed in your browser.
Flex source code
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
3 <mx:WebService
4 id="userRequest"
5 wsdl="http://www.gnpcb.org/esv/share/soap/index.php?wsdl">
6
7 <mx:operation name="doPassageQuery" resultFormat="object"
8 fault="mx.controls.Alert.show(event.fault.faultString)"
9 result="showResult(event)"/>
10 </mx:WebService>
11
12 <mx:Script>
13
14
15 <![CDATA[
16 import mx.rpc.events.ResultEvent;
17 import mx.rpc.events.FaultEvent;
18 import mx.controls.Alert;
19 [Bindable] private var passage:String;
20 private var options:Object = {"output-format": "plain-text"};
21
22
23 private function showResult(e:ResultEvent):void
24 {
25 passage = userRequest.doPassageQuery.lastResult;
26 }
27 private function send_data(input:String):void {
28 userRequest.doPassageQuery("IP",input, options)
29 // Alert.show("sending request");
30 }
31 ]]>
32
33 </mx:Script>
34 <mx:Text text="Flex Webservice Application - Query the ESV Bible" fontSize="16" fontWeight="bold"/>
35 <mx:Text text="Script verse to look up (example: John 3:16)" fontSize="12"/>
36 <mx:TextInput id="lookupPsg" textAlign="center"/>
37 <mx:Button label="Submit" click="send_data(lookupPsg.text)"/>
38 <mx:TextArea height="352" width="590" id="psg" text="{passage}" editable="false" fontSize="11"/>
39 <mx:Text text="The Holy Bible, English Standard Version copyright ©2001 by Crossway Bibles, a 
publishing ministry of Good News Publishers. 
Used by permission. All rights reserved." textAlign="center" width="590" height="52"/>
40
41 </mx:Application>
Notes
The webservice object is created using the flex <mx:webservice> element. The operation property of the element is the method called when the submit button is clicked. The other two properties of the element are the fault and result properties. The return of a good result is then bound to a variable using actionscript and that variable is bound to the text area. If a fault is returned that is shown using an alert.
There is no validation on the form. If you enter an invalid scripture verse the webservice sends back a message letting you know you requested an invalid passage. Since I was trying to use as little code as possible, I just used the default look and feel that comes out of a basic flex application. You have complete control over the look and feel of a flex application. Flex gives you a default look if you don't specify colors, fonts and layout. I am not using a server cgi at all. I am calling the webservice directly out of the application. Flex classes handle packaging the parameters into a soap request for you. I am requesting plain text as a return format. The ESV webservice will return xml or xhtml if you request that. You can return a single passage or any number of passages. If you want the first four books of Matthew you can request Matthew 1-4. Or you can request multiple passages like Isaiah 53:1-6. The ESV webservice has additonal methods. I am utilizing only one method in this application - doPassageQuery.
I have just realized that ESV webservices no longer offer SOAP services. I'm working on a REST http service fix for this. I'll post the changes soon. Brad - 1-2009.


Mon, 11/10/2008 - 02:47
Thanks a Lot for your work. It helped me a lot in understanding Flex + Web Services.
Haj.-
Thu, 10/23/2008 - 23:10
I'm going to be using Flex to connect to the java web service it is returning bytes[] (pdf) now my Queston is how to flush() that bytes in flex (to display pdf report) by consuming webservice .
Thanks
Kiran M
Fri, 08/15/2008 - 14:35
what if you have multiple inputs?
Mon, 04/21/2008 - 11:28
I was wondering if I needed one myself, but no. I didn't use one, unless the service has one installed.
--
Brad Rice
site designer
Mon, 04/14/2008 - 00:02
Thanks for this example. Are you using a crossdomain.xml file, or is one necessary?