This class uses Google Suggests to fill an array with its suggestions;
Note : this class doesn’t work on a server because of the sandbox violation;
But if you rum the application on your local machine or in an AIR project it works well
The Application:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="init()"
layout="vertical" xmlns:ns1="it.creativesource.*"
width="380" height="330">
<mx:Script>
<![CDATA[
import it.creativesource.GoogleSuggests;
private var gs:GoogleSuggester ;
private function init():void{
gs=new GoogleSuggester ();
gs.addEventListener(gs.SUGGESTED,displayResults)
}
private function findSuggests(e:KeyboardEvent):void{
output.htmlText='';
gs.findSuggests(e.currentTarget.text)
}
private function displayResults(e:Event):void{
for(var i:int=0;i<e.currentTarget.dataProvider.length;i++)
output.htmlText+=e.currentTarget.dataProvider[i]+'\n';
}
]]>
</mx:Script>
<mx:FormItem label="Write here :">
<mx:TextInput keyUp="findSuggests(event)" x="309" y="77"/>
</mx:FormItem>
<mx:FormItem label="Responses :">
<mx:TextArea id="output" x="309" y="105" height="238"/>
</mx:FormItem>
</mx:WindowedApplication>
The Class : - GoogleSuggester
package it.creativesource
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
[Event(name="suggested", type="flash.events.Event")]
public class GoogleSuggester extends Sprite
{
public const SUGGESTED:String='suggested'
private var _dataProvider:ArrayCollection=new ArrayCollection();
public function GoogleSuggester ()
{
}
public function findSuggests(string:String):void{
var hs:HTTPService=new HTTPService();
hs.url="http://www.google.com/complete/search?hl=en&client=suggest&js=true&q="+string+"&cp="+string.length;
hs.addEventListener(ResultEvent.RESULT,parseResults)
hs.send()
}
protected function parseResults(e:ResultEvent):void{
var tmp:Array=e.result.split('new Array')[1].split(', "');
_dataProvider=new ArrayCollection();
for (var i:int=1;i<tmp.length-1;i+=2){
tmp[i]=tmp[i].replace('"',"");
tmp[i]=tmp[i].replace('\\x27',"'");
_dataProvider.addItem(tmp[i]);
}
dispatchEvent(new Event('suggested'));
}
public function get dataProvider():ArrayCollection{
return _dataProvider;
}
}
}
