You Are Here:

Community: Wiki

This page was last modified on 13 September 2009, at 14:39.

How to create a Flash Lite weather application using Web services

From Forum Nokia Wiki

Reviewer Approved   

Contents

Introduction

Like any other application development technology, such as Symbian C++ or Java ME, Flash Lite can connect to a Web service and thus send/receive data. This article illustrates again the power of Flash Lite.


Concept

Here is a reference to the WSDL page on Wikipedia. To learn on the WSDL concept in detail, you may refer to this page. To learn more about its implementation in flash Lite, read below.

Image:Diagram1.png

Connecting to the Web service

Image:Weatherloading.jpg

The client's device establishes communication with the Web services by creating a new Web service object in the ActionScript and invokes its GetWeather() method once the "GO" button is pressed.

import mx.services.*;
// Create a new instance of Web service called - wthrService.
var wthrService = new WebService("http://www.webservicex.net/globalweather.asmx?WSDL");
 
// set it up so that the service is called when the button is pressed.
result_btn.onPress = function() {
loading_mc._visible = true;
//Call the getWeather method and assign it to the pending call object to handle results.
resultObj = wthrService.GetWeather(citybox.text, "");
wthrService.onLoad = trace("loading");
// the onResult function is called if the service is successful
resultObj.onResult = function(result) {
if(result ne "Data Not Found")
{
loading_mc._visible = false;
resStr = result.slice(result.indexOf("<CurrentWeather>")+16,result.indexOf("</CurrentWeather>")-17);
xml = null;
delete xml;
xml = new XML()
xml.ignoreWhite = true;
xml.parseXML(resStr)
cityTxt = citybox.text;
gotoAndStop(2);
}
else
{
gotoAndStop(3);
}
}
resultObj .onFault = function(fault) {
// If there is any error, e.g. the service is not working, the onFault handler will be invoked.
loading_mc._visible = false;
fault_txt = "ERROR: "+fault.faultCode + "," + fault.faultstring
}
}

XML parsing

The server sends the XML object containing weather information that must be parsed before it can be used.

- XML FORMAT

<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Islamabad Airport, Pakistan (OPRN) 33-37N 073-06E 508M</Location>
<Time>May 29, 2008 - 07:00 PM EDT / 2008.05.29 2300 UTC</Time>
<Visibility> 3 mile(s):0</Visibility>
<SkyConditions> clear</SkyConditions>
<Temperature> 77 F (25 C)</Temperature>
<DewPoint> 59 F (15 C)</DewPoint>
<RelativeHumidity> 53%</RelativeHumidity>
<Pressure> 29.65 in. Hg (1004 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>

The XML object may have more or less nodes as it depends on the level of information available on the server at any given time. Hence using the following code, the XML node's name/value pairs are stored in an array of a dataclass object.

- DataClass

class dataClass
{
//Establishes the variables in dataClass
public var nodeName: String;
public var nodeValue:String;
}
import dataClass; 
 
for (var aNode:XMLNode = xml.firstChild; aNode != null; aNode=aNode.nextSibling) {
if (aNode.nodeType == 1) {
var t:dataClass = new dataClass();
t.nodeName = aNode.nodeName;
t.nodeValue = aNode.firstChild.nodeValue;
array.push(t);
}
}

Trimming string data

Once data is saved in the array, it can be retrieved from the array and trimmed to be displayed on the screen.

for(j=0;j<array.length;j++)
{
if(array[j].nodeName eq "Time")
{
getLocalTime = array[j].nodeValue;
getLocalTime = getLocalTime.slice(0,getLocalTime.indexOf("EDT"));
}
if(array[j].nodeName eq "Wind")
{
getWind = array[j].nodeValue;
getWind = Number(getWind.slice(getWind.indexOf("at ")+3,getWind.indexOf("MPH")-1));
}
if(array[j].nodeName eq "Visibility")
{
getVisibility = array[j].nodeValue;
getVisibility = getVisibility.slice(0,getVisibility.length-2);
}
if(array[j].nodeName eq "Temperature")
{
getTemperature = array[j].nodeValue;
getTemperature_F = getTemperature.slice(0,getTemperature.indexOf("F")-1);
getTemperature_C = getTemperature.slice(getTemperature.indexOf("(")+1,getTemperature.indexOf("C")-1);
}
if(array[j].nodeName eq "DewPoint")
getDewPoint = array[j].nodeValue;
if(array[j].nodeName eq "RelativeHumidity")
getRelativeHumidity = array[j].nodeValue;
if(array[j].nodeName eq "Pressure")
{
getPressure = array[j].nodeValue;
getPressure = getPressure.slice(getPressure.indexOf("(")+1,getPressure.length-1);
}
}


Temperature gauge animation

new mx.transitions.Tween(temp_mc.temp_lvl_mc, "_height", mx.transitions.easing.Strong.easeOut, 0, getTemperature_C / 100 * 100, 5, true);


Final output

Image:Weather.jpg

Source code

The source code files are available for download: media:FLWeather.zip


Author

--NINJA 03:16, 30 May 2008 (EEST) RAHEAL AKHTAR

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHowE5ftoE5fgetE5fstartedE5fwithE5fFlashE5fE4citeE5fdevelopmentX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxHowE20toE20getE20startedE20withE20FlashE20E4citeE20developmentE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZflashQRqmarsZrelevanceQNx100X qfnZtopicQUqfnTopicZflashE5fliteQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d03X qfnZuserE5ftagQSxflashX qfnZuserE5ftagQSxflashE2dliteX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X