AboutFlex.net

flex,air,flash …


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;
		}

	}
}
  1. Mário Said,

    That’s a nice app, but you have a small error on the class at:

    tmp[i]=tmp[i].replace(’”‘,”);

    should be:

    tmp[i]=tmp[i].replace(’”‘,”");
    :)

    With our license i will make a version with server-side script to retrieve the suggestions to make it work on online mode to pass the sandbox restriction.

    i’ll give you news when ready! :)

  2. Fedele Marotti Said,

    Thanks Mario,
    I’ve updated the post;
    thanks for your observation;
    that error was due to a wordpress bug that converts two single quotes in a double quotes;

    Let me updated on your work :)

  3. Mário Santos Said,

    Hi Fedele Marotti!

    So, i’ve worked on a alternative to work around sandbox restriction, i made some minor changes to your code & class, and also made a script (send.php) that make the bridge from flex and google.

    I’ve keeped the credits for your work (in the class file), and posted on my blog a working example, feel free to download the source and update your work with both methods! :)

    So, you can see it working on:
    http://www.msdevstudio.com/blog/flexSource/testGoogeSug/

    Now it works on server and desktop mode :)

    p.s. Nice Blog!

Add A Comment