Server Communiation

Create unique Flash widgets you can import into SmartBuilder

Moderator: Nav

Server Communiation

Postby ScottA » Mon Jan 26, 2009 11:45 am

I am trying to figure out how to use the Server Communication widget. I have a web service that I am trying send a parameter and receive the result which is sent back as an XML string.

In my example below:
webservice name = webservice.asmx,
operation = GetChildrenCategories,
parameter = CategoryId,
parameter value = 0.

On a test SmartBuilder lesson, I have a button and I set onClick to do the following:
-Server Communication - setParameter = "CategoryId", "0"
-Server Communication - sendToURL = "the/url/to/my/webservice.asmx/GetChildrenCategories"

Now I'm stuck. I would like to see the XML string that the web service returns displayed in a text box that I also have on this test page. How do I receive the data that is returned? Something needs to be set as a listener for this return/load event, but I can't figure out what.

Also, when it is returned, I will need to parse it. Is there an XML widget that collects this data? How do I go about getting the data from the returned string once I get it?
Scott Allison
Veterinary Information Network
ScottA
 
Posts: 51
Joined: Sat May 26, 2007 4:22 pm
Location: Davis, California

Re: Server Communiation

Postby Nav » Tue Jan 27, 2009 10:54 am

Hi Scott,
I'm a bit hazy on the structure of server-side scripts. Can you tell me how you would normally send this parameter to your server, without using SmartBuilder (e.g. in a browser address bar)? Would it be something like this?
the/url/to/my/webservice.asmx?query=GetChildrenCategories&CategoryId=0

As soon as you can figure out how to do it without SmartBuilder, I can show you how to do it with SmartBuilder.

We have had luck in the past treating the operation like another parameter value, and the query as the parameter, but again it depends entirely on what your server is expecting. In previous cases, this has been the format:

webservice name = webservice.asmx,
operation = GetChildrenCategories,
parameter = CategoryId,
parameter value = 0

Code: Select all
-Server Communication - setParameter = "query", "GetChildrenCategories"
-Server Communication - setParameter = "CategoryId", "0"
-Server Communication - sendToURL = "the/url/to/my/webservice.asmx"

As for the response, how much information are you sending and returning? Are you sending, say 15 CategoryIds at once, or just 1 at a time? What is the form of the data being returned? There is no widget that can parse XML at the moment, but if it seems like it might be useful to you and other people, we can see about building one.

Setting the response to output to a text object is easy. Once you know how to get data back from the server, all you have to do is set up a response like the following:
Code: Select all
-Server Communication - setParameter = "query", "GetChildrenCategories"
-Server Communication - setParameter = "CategoryId", "0"
-Text1.setText = Server Communication - sendToURL = "the/url/to/my/webservice.asmx"

Basically, before I can really give you any tailored advice, I need to know what your server is expecting, and what it is returning. The best way to do this is without using SmartBuilder. Then we can make SmartBuilder treat it accordingly. Let me know!

- Nav
Nav
 
Posts: 866
Joined: Mon Nov 05, 2007 2:58 pm

Re: Server Communiation

Postby ScottA » Tue Jan 27, 2009 1:12 pm

oops, misspelled the title...embarrassing.

We generally use web services that have been created by our server-side/db people here. For example, we have a web service for our flashcards: flashcards.asmx. One operation in this web service is called 'GetChildrenCategories.' It takes one parameter: 'CategoryId.' A simple way to access this in Flash (as2) is doing the following:

var myXML:XML = new XML();
myXML.onLoad = myXMLLoadHandlingFunction();
myXML.load("flashcards.asmx/GetChildrenCategories?CategoryId=0");

the XML that is returned by this operation (abbreviated) is:

<?xml version="1.0" encoding="utf-8" ?>
- <ArrayOfFlashCardCategoryList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://.../webservices/">
<FlashCardCategoryList>
<CategoryID>1</CategoryID>
<Name>Anatomy & Physiology</Name>
<CardsInCategory>0</CardsInCategory>
<NumChildren>1</NumChildren>
</FlashCardCategoryList>

...

<FlashCardCategoryList>
<CategoryID>37</CategoryID>
<Name>Urinary Tract</Name>
<CardsInCategory>0</CardsInCategory>
<NumChildren>0</NumChildren>
</FlashCardCategoryList>
</ArrayOfFlashCardCategoryList>

In Actionscript, this is loaded as an XML object that can then be parsed.
I have also used the LoadVars object in as2 to do this too.

If Flex, I can use the WebServices object and give it the wsdl and the operation name. The returned results can then be formatted as e4x xml or a generic object.

I am seeing if I can do the same with the Server Communication widget in SmartBu
Scott Allison
Veterinary Information Network
ScottA
 
Posts: 51
Joined: Sat May 26, 2007 4:22 pm
Location: Davis, California

Re: Server Communiation

Postby Nav » Tue Jan 27, 2009 3:45 pm

Hi Scott,
Here's some background:
In Smartbuilder, the Server Communication object doesn't have anything to do with ActionScript. It uses http GET query protocol to send the request directly to the server.

Also, the data returned from the server is always treated as a string.

When you call Server Communication > sendToURL = "url/of/your/choice", it automatically waits for a response from the server. If it doesn't get a response from the server, it will hang the SmartBuilder lesson and you'll have to close it down. No need to set up listeners.

That being said, in your demo lesson set up button and a text object, and the following action:
Trigger: Button 1 > onPress
Response: Server Communication > setParameter (name = "CategoryID" ; value = "0")
Response: Text 1 > setText > Server Communication > sendToURL > "flashcards.asmx/GetChildrenCategories"


This will basically send the URL that you provided flashcards.asmx/GetChildrenCategories?CategoryId=0, and automatically set the text of that text object to whatever is returned (which will be your long XML string).

In your case, you should have (or will need to set up) an XML widget that can use that string. Instead of Text 1 > setText, you would likely either directly use a "getXML" method on your hypothetical widget, or set the value of a variable to that XML string, and then use the getXML method on that variable.

Does that make sense? Let me know if you are having trouble getting a text object to show the XML. We can set up a screen sharing meeting to try to troubleshoot it.

- Nav
Nav
 
Posts: 866
Joined: Mon Nov 05, 2007 2:58 pm

Re: Server Communiation

Postby ScottA » Wed Jan 28, 2009 9:46 am

Thanks,

I set it up like you said and it worked! The part I was missing was how to set up the text field to get the returned xml string. I didn't know that setting it up like that automatically made it a listener for the webservices load event. It only works when the SmartBuilder lesson is put on the same domain as the webservices. Now I need to work on an XML widget to parse the data. Thanks again.
Scott Allison
Veterinary Information Network
ScottA
 
Posts: 51
Joined: Sat May 26, 2007 4:22 pm
Location: Davis, California

Re: Server Communiation

Postby Nav » Wed Jan 28, 2009 10:27 am

Scott,
Glad to be of service. FYI, the URL Call widget works in a very similar manner to the built in Server Communication "object", however, it can work across domains, and it does not wait for a response for the server. This probably isn't something you'll use, but can come in handy if you have a public-facing lesson and want to capture some data from them. Happy authoring!

- Nav
Nav
 
Posts: 866
Joined: Mon Nov 05, 2007 2:58 pm


Return to Using the Object SDK

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Not able to open ./cache/data_global.php