Google Search in Flex and ActionScript 3.0
As often happens, i got the expiration to write this little post after an user request from an actionscript.it forum.
In few words, user asked how integrate a Google Search inside its Flex application to display the result always inside the flash app.
So I started to surf around the web to find a valid solution, and after founding many PHP solutions I found a cool AS3 library, developed by Boulevart.
This API provides a lot of classes to connect Flex (and Flash too) to Google services (Google Search, Translation, News, and so on) and require few code line to develop nice applications.
Following a Google Search Sample. Click on “Go” button to get more results:
THE SOURCE CODE:
<?xml version="1.0" encoding="utf-8"?> <!-- Use Google Search Bouleaver API http://labs.boulevart.be/index.php/2008/12/15/google-as3-api/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="600" height="500"> <mx:Script> <![CDATA[ import be.boulevart.google.ajaxapi.translation.GoogleTranslation; import com.adobe.utils.StringUtil; import be.boulevart.google.ajaxapi.search.web.GoogleWebSearch; import be.boulevart.google.ajaxapi.search.web.data.GoogleWebItem; import be.boulevart.google.ajaxapi.search.GoogleSearchResult; import be.boulevart.google.events.GoogleApiEvent; private var start:int=0 private var max:int=0 private var lang:String="" private var __results:Array private function searchWeb():void{ var google:GoogleTranslation = new GoogleTranslation() var googleWebSearch:GoogleWebSearch // Only first time if(max==0){ // Max result to get max=nmsResults.value __results=new Array() // Trim() method removes whitespace from the front and the end of the specified if(StringUtil.trim(txtInput.text).length){ // If italian radioButton is selected if(optItalian.selected){ lang="it" } // Start a new Search and waiting for a response googleWebSearch = new GoogleWebSearch() googleWebSearch.search(txtInput.text,0,lang) googleWebSearch.addEventListener(GoogleApiEvent.WEB_SEARCH_RESULT,onWebResults) } }else{ // Start a new Search and waiting for a response googleWebSearch = new GoogleWebSearch() googleWebSearch.search(txtInput.text,start,lang) googleWebSearch.addEventListener(GoogleApiEvent.WEB_SEARCH_RESULT,onWebResults) } } private function onWebResults(e:GoogleApiEvent):void{ var stop:Boolean=false var resultObject:GoogleSearchResult=e.data as GoogleSearchResult trace("Estimated Number of Results: "+resultObject.estimatedNumResults) trace("Current page index: "+resultObject.currentPageIndex) trace("Number of pages: "+resultObject.numPages) // Loop over the result for each (var result:GoogleWebItem in e.data.results as Array){ trace(result.title,result.url) start++ if(start <= max){ __results.push(result) }else{ stop=true } } // make a new search if start < Max if((start<=(max)) && !stop){ searchWeb() } if(stop){ // Populate dataProvider webResultsView.dataProvider=__results max=0 lang="" start=0 } } ]]> </mx:Script> <mx:HBox> <mx:TextInput width="293" id="txtInput" text="ActionScript.it"/> <mx:NumericStepper minimum="3" id="nmsResults" stepSize="1" value="15" maximum="30"/> <mx:Button label="go" click="searchWeb()" /> </mx:HBox> <mx:CheckBox label="search Italian sites only" id="optItalian"/> <mx:TileList height="350" id="webResultsView" rowHeight="120" columnWidth="500" columnCount="1" right="10" left="10" top="50" bottom="10" itemRenderer="WebItem"></mx:TileList> </mx:Application>











Leave your response!