Yahoo! Answers API Example
by Hepp Maccoy, Senior Flash Engineer UED
Yahoo! Answers is the web's leading Q&A service with over 100 million answers in its database. All of that data is readily available via Yahoo! Answers web-based API and our Answers AS3 Library. The example in this article demonstrates how to use the AS3 library to quickly build an application that allows you to browse and retrieve questions and answers from the Yahoo! Answers database.
What You Need
- Adobe Flex Builder 2 (Trial version available here)
- Solid knowledge of ActionScript 3 and/or MXML
- How to setup Yahoo! ASTRA Web APIs library
- Answers 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
The Answers Browser
This is a simple example, which demonstrates how to utilize the Answers API within Flex Builder 2. This application is a browser for the Answers content, allowing you to search for specific questions. Once you have made a search, the resulting questions (if results are yielded), will show below the search box, allowing you to select them individually.
Once an individual question is selected, the application displays two additional panels on the right. The top panel will show the full question content, and the panel below it will show all the answers for this question (if they exist).
Answers API Communication
How do we set up the communication with the Answers API? The Answers API contains a class, AnswersService, which serves as the API communication entrypoint. It offers the following functions:
The calls to all of these functions are event-driven and asynchronous. When you call searchQuestionsByTerm, the function returns nothing; however, once the data is obtained and parsed, the AnswersService dispatches an appropriate event with the data contained in the Event object. Let's set that up.
The Application Setup
Setting up the application is also quite simple. In the following code, we instantiate the AnswersService and add an event listener to it, listening for the specific event type (the list of event types is available as constant member variables of the AnswersResultEvent class):
Then we simply call the method that makes a call for the necessary data:
View this example live in your browser.
Download the Yahoo! ASTRA Web APIs library to access full source code for this example.
Summary
Yahoo! Answers offers huge potential as a pipeline of global information on a plethora of subjects. This is the best of user-generated-content, with over 60 million users posting a wealth of knowledge on every subject on a nearly daily basis. With our Yahoo! ASTRA Web APIs library, you can now very easily make this data work within your Flash and Flex creations, using the information in any context you can imagine. Here are some ideas to get you started:
- A News browser that links keywords to Yahoo! Answers
- A music application that links artist names to questions about them on Yahoo! Answers
- A blog reader that displays relevant Yahoo! Answers next to keywords
Also see: Weather and Answers Mashup example