The First and Only Magazine for Macromedia MX™
   
 
Notes from the Flash, Flex and ColdFusion trenches

Category List

Quick Poll

Where are you located?
North America
South America
Europe
Asia
Africa
Australia

My RSS Feeds








Hit Counter

Total: 1,305,972
since: 20 Jan 2005

Search Box

 

Search The Web

Google

MAX 2008

Fun Stuff

Mailing List

Got A Question?

Got A Question?

Leave a comment by the appropriate Entry, or email me

Tag Cloud

                                                                                                                       

Using Amazon's ItemSearch WebService from Flex

posted Saturday, 1 October 2005

I recently saw someone asking how to use the Amazon search service from Flex.  The only real challenge is reading through their docs and finding the right format for the parameters sent to them.  You can see the proper structure in the doSearch() method below.

The only other tricky item is the structure of the data being returned. The results contain an array of items. Each item has an id (ASIN), url (DetailPageURL) and an object called ItemAttributes. To display the title and authors of the books in the results, you need to use a label function to return Item.ItemAttributes.Author or Item.ItemAttributes.Title You can find this in the showTitle and showAuthor methods below.

I also added an event handler for the change event, which uses the DetailPageURL to show the full product page on amazon

Hope you find this helpful

<?xml version="1.0" encoding="iso-8859-1"?>
<mx:Application width="800" height="600" xmlns:mx="
http://www.macromedia.com/2003/mxml
creationComplete="appInit()">

<mx:Script>
<![CDATA[
    import fast.echo.*;
    var subId:String = ""; // your subscription id here
   
function showResult(event:Object){
     items.dataProvider = event.result.Items.Item;
   }
   function showTitle(item:Object):String{
      return item.ItemAttributes.Title;
   }
   function showAuthor(item:Object):String{
      return item.ItemAttributes.Author;
   }
 
  function showBook(url:String){
      getURL(url,"_new");
   } 
   function doSearch(){
      var searchParams:Object = new Object();
      searchParams.SearchIndex = "Books";
      searchParams.Sort = "salesrank";
      searchParams.SubscriptionId = subId;
      searchParams.Keywords = searchTerm.text;
      var search:Object = new Object();
      search.SubscriptionId = subId; 
      search.Request = searchParams; 
      amazonService.ItemSearch(search);
   }
]]>
</mx:Script>
<mx:WebService 
    wsdl="
http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl"
    id="amazonService" 
    result="showResult(event)" 
    showBusyCursor="true"/>
<mx:HBox width="100%">
    <mx:TextInput id="searchTerm" width="50%"/>
    <mx:Button click="doSearch()" label="Search"/>
</mx:HBox>    
<mx:DataGrid 
    id="items" 
    width="100%" 
    change="showBook(event.target.selectedItem.DetailPageURL)">     
    <mx:columns>
        <mx:Array>
            <mx:DataGridColumn columnName="title" labelFunction="showTitle" width="400" />
            <mx:DataGridColumn columnName="Author" labelFunction="showAuthor" width="150"/>
            <mx:DataGridColumn columnName="ASIN" headerText="ID" width="75"/>
        </mx:Array>
    </mx:columns>
</mx:DataGrid>
</mx:Application>

tags:    

links: digg this    del.icio.us    technorati    reddit




1. AndyC left...
Tuesday, 22 November 2005 2:56 pm

I'm not logged in so that may be why I cannot see any comments However, your code appears to be missing the appInit()function Also. Have you tried this version in flex 2 yet? cheers


2. Jeff Tapper left...
Tuesday, 22 November 2005 3:18 pm

right you are. you can remove the reference to it from the Application tag. Havent yet tried a Flex 2 version, but will post a blog when i do


3. Mike left...
Sunday, 15 July 2007 11:33 pm :: http://www.developingwithadobe.com

I tried this code in flex builder but I get a response back from amazon saying:

Your request should have atleast 1 of the following parameters: AWSAccessKeyId, SubscriptionId.


4. Mike left...
Sunday, 15 July 2007 11:39 pm :: http://www.developingwithadobe.com

Just to clarify I changed this line of code:

search.SubscriptionId = subId;

to

search.AWSAccessKeyID = "I did specify my access key";

because amazon doesn't issue subscription Id's anymore. You have to use your Access key