gofr: AS3 Web Service

November 25th, 2010 Leave a comment Go to comments

Introduction

gofr is the new project name for my AS3 web service package. I figured “AS3 Web Service” was a little lengthy, and using code names are really cool. ;)
This will be the new home for this package. You will be able to read up on any updates or new downloads.

Features

  • Queued service calls: Methods will be executed in the order they are called in.
  • Assign callbacks: You can assign a function to execute for every service call you make when the response has loaded.
  • Automatic SOAP requests: The class will build and send the SOAP requests for you, no need to create chunks of XML manually.
  • Optional Caching: Saves responses for faster access.

Releases

  • [11.14.07] gofr 1.1 – Click To Download
    - Added optional caching feature
    - Added support for Java based servers using X-Fire

Usage

Simple web service calls

//Required imports
import alducente.services.WebService;
import flash.events.*;
//Instantiate the class
var ws:WebService = new WebService();
//Add a listener for when the web service methods become available
ws.addEventListener(Event.CONNECT, connected);

//Connect to the service by downloading the WSDL
ws.connect("http://ws.cdyne.com/ip2geo/ip2geo.asmx?WSDL");

//Once connected, call an available method on the service named "ResolveIP" with the appropriate parameters
function connected(evt:Event):void{
	ws.ResolveIP(done, "192.123.0.200", 0);
}

//This function is called when there is a response from the sesrvice
function done(serviceResponse:XML):void{
	trace("nWeb Service Result: ");
	trace(serviceResponse);
}
  1. January 31st, 2011 at 06:28 | #1

    I solve it as below :)
    var Result = serviceRespone.children()[0].children()[0].children()[0]
    but is this valid for any response ???

  2. February 4th, 2011 at 13:33 | #2

    Thank you for this class. I was hoping someone could help me out, I’m trying to use the National Forcast Data Feed (http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl) to test.

    I’ve been trying to use their functions like “NDFDgen” like this: “ws.NDFDgen(done, “60302″, 0 )” but I get an error: “TypeError: Error #1006: NDFDgen is not a function.”

    The webservice connects but I can’t call any of their methods. I tried to trace out ws.availableMethods[0] (using the availableMethods method in the WebService class) but I get the error: “TypeError: Errpr #1010: A term is undefined and has no properties.”

    Does anyone know what I’m doing wrong?

    Thanks for your time!
    -Justin

  3. February 24th, 2011 at 14:34 | #3

    Hi, I’m also getting an error. The availableMethod function returns an empty array because because of the lacking support of xsd:import/external schemaLocation.Is there a way to fix that? That would be geat!

  4. February 24th, 2011 at 15:24 | #4

    @Justin: The problem is caused by this “” line.

  5. February 24th, 2011 at 15:29 | #5

    ok, second try, by the line: xsd:schema targetNamespace= …

  6. karn
    March 21st, 2011 at 13:17 | #6

    I have the same problem anybody find the solution?
    thank for you help!

  7. March 31st, 2011 at 03:04 | #7

    Why It is not working if swf and webservice both are kept on different domains. ?? Even i provided the crossdomain.xml file.

  8. May 3rd, 2011 at 12:50 | #8

    The next step, which seems to be causing some of you trouble, is to properly parse the XML using namespaces (its a bit akward, but is well documented. you need to declare a namespace variable and use a different form of addressing to get the data, but once you have it setup its seamless). search for “AS3 XML namespace”

  9. May 17th, 2011 at 10:32 | #9

    Awesome. Thank you much needed class library!

  10. June 8th, 2011 at 11:46 | #10

    Hi Carlo, you are a genius! You saved me a lot of days of work, thank u very much.
    I want to contribute fot those that doesn’t want namespaces in the resultant xml. I used regular expressions to strip off the unnecessary (and noisy) namespaces like soap, xmlns, and xsi, this is the code, where “xml” is the xml resultant from a web service function:
    var re1:RegExp = new RegExp(“xmlns[^\"]*\”[^\"]*\”", “gi”);
    var re2:RegExp = new RegExp(“soap:”, “gi”);
    var re3:RegExp = new RegExp(“xsi:”, “gi”);
    var str:String = xml.toString();
    str = str.replace(re1, “”);
    str = str.replace(re2, “”);
    str = str.replace(re3, “”);
    var tempXML:XML = new XML(str);

    enjoy!
    Fabio

  11. 80prozent
    June 14th, 2011 at 13:21 | #11

    very cool classes! thx for sharing!

    the resolveIP-example works very well.
    when i connect to the webservice i need for my project, it successfully connects, but i does not seem to react on any method-call i make. no response at all.

    any ideas?

    sorry for bad english

  12. Dan
    June 15th, 2011 at 00:42 | #12

    Thanks for this awesome class, it’s much better than using the flex one which from memory adds almost 300kb to your .swf. Was wondering if someone could give me some help on handling errors though? for example every now and then if my webservice goes down i get “Error #2044: Unhandled ioError:. text=Error #2032: Stream Error.”… I would like to handle this gracefully in flash. I tried ws.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler) and also putting my webservice function calls in a try / catch block but flash still reports the error as unhandled.

  13. alexander birchler
    June 16th, 2011 at 06:57 | #13

    Hi, Thanks for the code
    I have a problem, I want to send one variable to webservice not to read, but I cant. in fact I get the error 1080
    the error is:
    TypeError: Error #1080: Illegal value for namespace.
    at alducente.services::WSDL/getPortType()
    at alducente.services::WSDL/wsdlLoaded()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()
    I need to send a variable to the webservice using soap, any help?
    the variable name must be FileName

  14. Stefano
    June 17th, 2011 at 14:40 | #14

    Hi Carlo,

    I am using your classes and they work very well. I had a problem for WSDL that use the functionality (which is pretty common nowadays) and I modified your code to handle imports. I am not an AS developer and I did not test it extensively, but it works for me. I post it here the functions that I modified in WSDL.as. I hope it helps.

    public function WSDL(path:String){
    __wsdlPath = path;
    __rawWSDL = null;
    }

    private function wsdlLoaded(evt:Event):void{
    var xml:XML = new XML(evt.target.data);

    //Handle import
    var wsdl:Namespace = xml.namespace();
    var xmlImport : XMLList = xml.child(“*”);
    var importAddress:String = null;

    if ( xmlImport != null ) {
    var nameImport:String = wsdl.uri + “::import”;

    for each (var element : XML in xmlImport ) {
    //element.ignoreProcessingInstructions = false;
    if( element.name() == nameImport ){
    for each (var attr : XML in element.attributes()) {
    if ( attr.name() == “location” ) {
    // The statement should be removed before copying xml to __rawWSDL
    deleteNode(element);
    importAddress = attr.toString();

    trace(“Loading import wsdl ” + importAddress);
    }
    }
    }
    }
    }

    if ( __rawWSDL == null ) {
    __rawWSDL = xml;
    }else{
    for each (var child in xml.child(“*”)) {
    __rawWSDL.appendChild(child);
    }
    }

    if (importAddress == null) {
    read();
    }else {
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, wsdlLoaded);
    urlLoader.addEventListener(IOErrorEvent.IO_ERROR, wsdlNotLoaded);
    urlLoader.load(new URLRequest(importAddress));
    }
    }

    private function read() : void {
    trace(“final XML ” + __rawWSDL.toString());
    var portType:String = getPortType(__rawWSDL);
    var bindingType:String = getBinding(portType);
    __methodList = getMethodList(bindingType);
    __availableMethods = getAvailableMethods(__methodList);
    if (__availableMethods != null) {
    __parseComplete(__availableMethods);
    }

    }

    private static function deleteNode(node:XML):void
    {
    // credit to http://edsyrett.wordpress.com/2008/07/26/xmlremovechild/
    if (node != null && node.parent() != null)
    {
    delete node.parent().children()[node.childIndex()];
    }
    }

  15. Matt
    June 17th, 2011 at 16:29 | #15

    Hi Carlo, Just wanted to thank you for creating this and releasing it to everyone! You just saved me an incredible amount of time. Keep up the good work!

  16. Matt
    June 17th, 2011 at 16:33 | #16

    Also, put some ads on your blog. You deserve to be paid for this. I would happily buy anything you were selling right now.

  17. July 25th, 2011 at 22:20 | #17

    nice job, thanks!

  18. July 25th, 2011 at 23:18 | #18

    @abed allateef qaisi

    awesome job! Returns the result of the webservice perfectly!

  19. July 29th, 2011 at 16:36 | #19

    Thank you so much for the hard work you did on this class. It’s a great framework for how to work with SOAP in flash. I just want to make a note to everyone with questions. Not every webservice is going to have functions like resolveIP as part of their funcitonality. You are going to need to break down the framework here and match it to your development needs.

    Thanks again for the hard work!

    -Moto

  20. Aeolus
    August 5th, 2011 at 13:00 | #20

    when connect to the webservice I make by java , it say that “TypeError: Error #1006: sayHelloToWS is not a method . Who can help me?

  21. Alejandro
    September 27th, 2011 at 09:17 | #21

    Hi. Great job.
    I need oyur help. When the site thath host webservice is down, or not internet connection, tha call throw an error in getWSDL method in WSDL class.

    I try t catch them, with this code block in WebService connect method:

    public function connect(wsdl:String):void{
    try {
    __wsdl = new WSDL(wsdl);
    __wsdl.getWSDL(wsdlComplete);
    }
    catch (error:Error) {
    throw new Error(“No se pudo guardar el proyecto”);
    }
    }

    and this code block in my application:

    try {
    ws.addEventListener(Event.CONNECT, conectado);
    ws.connect(“http://www.fotodepot.com.ar/GuardarProyecto.asmx?WSDL”);
    }
    catch (error:Error) {
    popup.CerrarVentana();
    popup = new VentanaEditor();
    popup.show(‘No hemos podido guardar su proyecto’);
    }

    Thoes not work. The error still launching and the catch block never is executed.

    Can you help me.

    Regards.

    Alejandro

  22. October 6th, 2011 at 11:31 | #22

    Hi there,

    I’m seeking for a good soap service and your’s seems to be very clean and easy to use. Now i have to connect to a service and the wsdl file is loading perfectly. But there is also a XSD file who is describing the methods and i’m not sure your library supports external XSD files. Can u please confirm this.

    Thank you so much!

  23. silverfox
    December 17th, 2011 at 20:43 | #23

    it is possible to send data?

    I want to send me a SOAP server and returns another SOAP, XML is that I will send 20mb, is this possible?

  24. January 5th, 2012 at 11:19 | #24

    I was having the issue of function not found due to the lacking support of xsd import and what i had to do was as described on this link

    http://my-tech-talk.blogspot.com/2008/07/adding-flatwsdl-to-wcf-webservice.html

    I basically had to change my WCF service to flatten out the wsdl so there are no imports.

    @silverfox: i am sending XML my self to my webservice and they are quite big and it seems to work ok. i have not tried 20 MB yet but will try that.

  25. April 4th, 2012 at 05:34 | #25

    Hi i am getting error of TypeError: Error #1010: A term is undefined and has no properties.
    can you please have a look at my code

    import alducente.services.WebService;
    import flash.events.*;

    var ws:WebService = new WebService();
    ws.addEventListener(Event.CONNECT, connected);
    ws.connect(“http://localhost:8732/Design_Time_Addresses/TestService/Service1/?wsdl”);
    ws.cacheResults = true;

    var initTime:Number;

    function connected(evt:Event):void{

    ws.IBookService.getBooks(done,0,0);
    var obj:Array=ws.IBookService.GetBooks();

    }

  26. Mark Redman
    April 18th, 2012 at 05:02 | #26

    Hi, did you manage to find a solution to catch errors?

  27. Mark Redman
    April 18th, 2012 at 05:04 | #27

    My previous comment was referring to Alejandro’s message about connecting when the service is down. At the moment an exception is raised, I would like to handle the error gracefully.

Comment pages
1 2 11
  1. November 14th, 2007 at 17:08 | #1
  2. November 14th, 2007 at 17:10 | #2
  3. November 14th, 2007 at 17:15 | #3
  4. February 2nd, 2008 at 11:47 | #4
  5. July 7th, 2008 at 10:41 | #5
  6. November 25th, 2010 at 21:01 | #6
  7. November 25th, 2010 at 21:03 | #7
  8. November 25th, 2010 at 21:05 | #8