Using Amazon’s ItemSearch WebService from Flex

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>

There are no comments.

Leave a Reply