Yahoo! Search Application
by Alaric Cole, Senior UED/Prototyping Engineer
In this article, we will show you how to integrate Yahoo! Web Search into your Flash or Flex application. Yahoo! Search API consists of many "sub-APIs", all of which are represented in the AS3 Library. Whether you use MXML or ActionScript, the AS3 Library will allow you to get started quickly.
What You Need
- If working in Flex, Adobe Flex Builder 2 (Trial version available here)
- Basic knowledge of MXML and/or ActionScript
- Yahoo! ASTRA Web APIs library
- Search API Key, available here
All AS3 tutorials
- Mashup Yahoo! Weather and Answers
- Build an Upcoming.org Event Browser
- Make a Yahoo! Search Application
- Build a Yahoo! Answers Browser
Search Application in Flex / MXML
This is a simple example demonstrating how to use the Search API in a Flex application. Before you continue, make sure that you've followed the instructions on how to set up the Yahoo! ASTRA Web APIs library.
First, create a new MXML Application. Then, add the search service to your application, as follows. Note: If using code completion when entering the SearchService tag, the XML namespace will automatically be set for you. If necessary, add it manually.
Next, add your UI input elements, such as a TextInput:
Then bind the TextInput to the service you've already created:
Also, bind a List to the results of the search:
Finally, create a Button to call the send() method:
Now you have the power of Yahoo! Search instantly available in your application.
Search Application in Flash 9 / Pure AS3
First, import the required classes:
Next, declare a search service in ActionScript:
And declare a SearchParams object to hold the parameters:
Also, you'll want to set up methods for when the search has results, and possibly check for errors:
Set up the event listeners for the above methods:
You may want to encapsulate the search call in a function:
Finally, set up some UI elements to invoke the search, like text fields and buttons, and you're on your way!
View this example live in your browser.
Download the Yahoo! ASTRA Web APIs library to access full source code for this example.
Summary
This covers only the most basic usage of Yahoo! Web Search. Remember, however, that this API includes Image/Video/Audio search, News, Local search, as well as Term Extraction/Context Search, Spelling Suggestions, and many others. You could easily build a spell checker, news reader, media player, and countless others with this simple and robust API. Your only limit is your imagination.