December 15th, 2008Google API - AS3 Library

A while ago, I have build an API library in AS3 that interfaces with Google’s own javascript API. To show you how performant this API is, I included a demo-app at the bottom of this blog.
The library is a simple collection of the API’s features, there is a class per feature, like Google Web search, Images Search, etc. Each library throws it’s own event with an Array of matching datatypes, for example GoogleWebItem, so it’s easy to keep track on wich data is recieved and what this data contains.
This api contains:
- Google Web Search
- Google Images Search
- Google Book Search
- Google Video Search (Google Video & YouTube)
- Google Blog Search
- Google Local Search
- Google Patent Search
- Google News Search
- Google Translation
- Google Weather
- Google Suggest
This API is Flash & Flex compatible.
Little example code
var googleWebSearch:GoogleWebSearch=new GoogleWebSearch()
googleWebSearch.search(txtInput.text,0,lang)
googleWebSearch.addEventListener(GoogleSearchEvent.WEB_SEARCH_RESULT,onWebResults)
private function onWebResults(e:GoogleSearchEvent):void{
for each (var result:GoogleWebItem in e.data as Array){
trace(result.title, result.url)
}
}
The library
You can download the library here (updated).
The demo app can be found here.
Right-click in the app to see source-code.
No Tags




December 24th, 2008 at 6:21 am
thank you very much for makeing this very hand tool. it should speed things along nicely for me.
December 24th, 2008 at 9:40 am
woops i think i found a bug but is was simple to fix here look its in the GoogleLocalSearch.as
//item.title = item.titleNoFormatting = item.url = item.latitude = data.lat
item.title = data.titleNoFormatting
item.longitude = data.lng
item.latitude = data.lat
item.url = data.url
December 29th, 2008 at 11:24 am
Thx Chris for noticing,
weird thing happened there. guess I was getting sleepy…
Library has been updated.
January 6th, 2009 at 9:42 am
[...] 地址:http://labs.boulevart.be/index.php/2008/12/15/google-as3-api/ [...]
April 4th, 2009 at 1:19 am
[...] Boulevart.be Google API [...]
June 12th, 2009 at 2:46 am
Hey Joris,
I downloaded your googleas3api.swc to use on an experimental flash project of mine. I want to use it for the Google Images API. In a simple test, when just trying it out, i wrote the following code:
package
{
import be.boulevart.google.ajaxapi.search.images.GoogleImageSearch;
import be.boulevart.google.events.GoogleApiEvent;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import mx.utils.StringUtil;
public class HelloWorld extends Sprite
{
private var urlLoader:URLLoader;
public function HelloWorld()
{
var google:GoogleImageSearch = new GoogleImageSearch();
google.search(”dogs”);
google.addEventListener(GoogleApiEvent.IMAGE_SEARCH_RESULT, onSearchComplete);
}
private function onSearchComplete(e:GoogleApiEvent):void {
trace(”it worked”);
}
}
}
I got the following error:
ReferenceError: Error #1065: Variable StringUtil is not defined.
at be.boulevart.google.ajaxapi.search.images::GoogleImageSearch/search()[/Users/joris/Documents/Flex Builder 3/GoogleEyes/libs/be/boulevart/google/ajaxapi/search/images/GoogleImageSearch.as:60]
Any ideas? Thanks!
—
As mentioned in the post, the classes use the Adobe AS3 CoreLib, for Json Parsing and a few StringUtil usage, so download that swc, add it to your project and the classes will work fine.
Regards,
Joris
June 22nd, 2009 at 9:59 am
Thanks for this excellent API
I am trying out the Image Search. Same code as mentioned by John Martz.
I have inclued the AS3 core lib
Now I get the below error:
JSONParseError: Unexpected < encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:579]
at com.adobe.serialization.json::JSONTokenizer/getNextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:168]
at com.adobe.serialization.json::JSONDecoder/com.adobe.serialization.json:JSONDecoder::nextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:83]
at com.adobe.serialization.json::JSONDecoder$iinit()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:60]
at com.adobe.serialization.json::JSON$/decode()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSON.as:78]
at be.boulevart.google.ajaxapi.search.images::GoogleImageSearch/be.boulevart.google.ajaxapi.search.images:GoogleImageSearch::onResponse()[/Users/joris/Documents/Flex Builder 3/GoogleEyes/libs/be/boulevart/google/ajaxapi/search/images/GoogleImageSearch.as:91]
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()
Do you know whats going wrong here?
August 5th, 2009 at 2:55 pm
Hi Fasil, I seem to be getting near enough the same error as you -
JSONParseError: Unexpected < encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:579]
at com.adobe.serialization.json::JSONTokenizer/getNextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:168]
at com.adobe.serialization.json::JSONDecoder/nextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:83]
at com.adobe.serialization.json::JSONDecoder()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:60]
at com.adobe.serialization.json::JSON$/decode()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSON.as:78]
at main/getPHPData()[C:\wamp\www\ClassDB\src\main.mxml:25]
at main/__getData_result()[C:\wamp\www\ClassDB\src\main.mxml:58]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43]
at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74]
at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:403]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Any ideas anybody?
August 6th, 2009 at 12:16 pm
@Andrew & @Fasil
I’m afraid I’m going to need more details
what are your search parameters, etc…
tried escaping your search terms, just use the escape() function.
Google is giving you a wrong response, the results is propably of non-json formatting.
September 2nd, 2009 at 1:12 pm
Great work man..
i have tested the Google Search API and they work really well.
September 3rd, 2009 at 3:11 pm
[...] 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. [...]
September 7th, 2009 at 8:53 pm
Great job! Saved me loads of time while trying to visualize google search results.
Should ‘GoogleSearchEvent’ in the example snippet be ‘GoogleApiEvent’ instead?
September 7th, 2009 at 9:09 pm
oops.
Should’ve read the ‘Changes & new features’ under ‘Google API - AS3 library: v. 2.0′ before asking about ‘GoogleSearchEvent’ in the example snippet.
Sorry about that
November 11th, 2009 at 1:49 am
Thanks for the nice libraries. I was having trouble getting the example to work in the Flash authoring environment, but finally figured it out. I wanted to post the code in case it will help other flash noobs like myself figure this stuff out.
//START CODE
import be.boulevart.google.ajaxapi.search.web.data.*;
import be.boulevart.google.ajaxapi.search.web.*;
import be.boulevart.google.ajaxapi.search.*;
import be.boulevart.google.events.*;
//import be.boulevart.google.*;
//import com.adobe.utils.StringUtil;
var searchLanguage:String = “eng”
var googleWebSearch:GoogleWebSearch=new GoogleWebSearch();
googleWebSearch.search(txtInput.text,0,searchLanguage);
googleWebSearch.addEventListener(GoogleApiEvent.WEB_SEARCH_RESULT,onWebResults) ;
googleWebSearch.addEventListener(GoogleAPIErrorEvent.API_ERROR,onAPIError)
function onWebResults(e:GoogleApiEvent):void{
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)
for each (var result:GoogleWebItem in resultObject.results){
trace(result.title, result.url)
}
}
function onAPIError(evt:GoogleAPIErrorEvent):void{
trace(”An API error has occured: ” + evt.responseDetails, “responseStatus was: ” + evt.responseStatus);
}
November 11th, 2009 at 11:14 am
Hello,
I was able to get an image search working in flash. Thanks again for the great API.
Unfortunately, I am getting an error when the search loads ‘many’ images: “An API error has occured: out of range start responseStatus was: 400″.
I was wondering if there is documentation for this error, or if there is a high-limit for the start range for an image search.
November 11th, 2009 at 10:47 pm
Hi Madhat,
The error you are getting is an error given by the Google API, you probably bumped upon a restriction or they aren’t more images,
trying to get images from a a start index that is too great or does not exists probably gives this error…
November 12th, 2009 at 11:37 am
Thanks Joris,
Upon further testing, it appears that the 400 error is thrown when more than 64 images are loaded, or 8 iterations. Thanks for your reply.
April 18th, 2010 at 4:12 am
Congratulations on your great work, Joris.
I’m using your library to find books and I have problems for finding the images books. When I try to access the image returns me undefined.
For example, if i search Harry Potter:
private function onBookResults(e:GoogleApiEvent):void{
var resultObject:GoogleSearchResult=e.data as GoogleSearchResult;
for(var i:uint=0;i<resultObject.results.length;i++){
var libro:GoogleBook=resultObject.results[i];
trace(libro.authors); //Return J.K.Rowling
trace(libro.thumbnailHTML); //Return undefined
}
}
In the AJAX API documentation i found this:
“Especifica un nodo DOM HTML que representa una imagen en
miniatura de la portada del libro. Para utilizar esta
propiedad correctamente, debes clonar el nodo (por ejemplo,
var cover = res.thumbnailHtml.cloneNode(true);). Esta
propiedad no estará disponible si has suprimido la generación
HTML (con .setNoHtmlGeneration()).”
Do you think it is related?
Thanks for your help.
April 22nd, 2010 at 1:27 am
@David
Thanks for noticing, Google as changed some properties in their JSON responses.
thumbnailURL has been renamed to tbUrl, changed this in the next version, v3 of the API.
(pre-release available in a branch on Google Code)
April 24th, 2010 at 9:11 pm
Fantastic, Joris. Thank you very much. Now it works perfectly