Amazon.com Widgets

How To Call Amazon ItemSearch in Flex

Parsing the Resulting XML response

If your request is properly formatted, you’ll get an XML document back from the ItemSearch service similar to the following:

  1. <ItemSearchResponse>
  2. <OperationRequest>
  3. <HTTPHeaders>
  4. <Header Name=“UserAgent” Value=“Your user agent”/>
  5. </HTTPHeaders>
  6. <RequestId>1QP2MMWGCGK4TEXQ3Z72</RequestId>
  7. <Arguments>
  8. <Argument Name=“SearchIndex” Value=“Books”/>
  9. <Argument Name=“Sort” Value=“relevancerank”/>
  10. <Argument Name=“Service” Value=“AWSECommerceService”/>
  11. <Argument Name=“Keywords” Value=“Java”/>
  12. <Argument Name=“SubscriptionId” Value=“Your AWSID”/>
  13. <Argument Name=“Operation” Value=“ItemSearch”/>
  14. </Arguments>
  15. <RequestProcessingTime>0.154212951660156</RequestProcessingTime>
  16. </OperationRequest>
  17. <Items>
  18. <Request>
  19. <IsValid>True</IsValid>
  20. <ItemSearchRequest>
  21. <Keywords>Java</Keywords>
  22. <SearchIndex>Books</SearchIndex>
  23. <Sort>relevancerank</Sort>
  24. </ItemSearchRequest>
  25. </Request>
  26. <TotalResults>5761</TotalResults>
  27. <TotalPages>577</TotalPages>
  28. <Item>
  29. <ASIN>1592730051</ASIN>
  30. <DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=1592730051%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/1592730051%253FSubscriptionId=yourID</DetailPageURL>
  31. <ItemAttributes>
  32. <Author>David Brackeen</Author>
  33. <Author>Bret Barker</Author>
  34. <Author>Laurence Vanhelswue</Author>
  35. <Manufacturer>New Riders Games</Manufacturer>
  36. <ProductGroup>Book</ProductGroup>
  37. <Title>Developing Games in Java</Title>
  38. </ItemAttributes>
  39. </Item>
  40. <Item>
  41. <ASIN>0596009208</ASIN>
  42. <DetailPageURL>http://www.amazon.com/gp/redirect.html%3FASIN=0596009208%26tag=ws%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0596009208%253FSubscriptionId=yourId</DetailPageURL>
  43. <ItemAttributes>
  44. <Author>Kathy Sierra</Author>
  45. <Author>Bert Bates</Author>
  46. <Manufacturer>O’Reilly Media</Manufacturer>
  47. <ProductGroup>Book</ProductGroup>
  48. <Title>Head First Java, 2nd Edition</Title>
  49. </ItemAttributes>
  50. </Item>
  51.  
  52. <!– Other Items in the List Deleted for Space –>
  53.  
  54. </Items>
  55. </ItemSearchResponse>

Note the structure of the Items list. For each Item returned, there is a child Item element, and that has the children ASIN, DetailPageURL, and ItemAttributes. The key features that we think about such as Author and Title are actually children of the ItemAttributes element, not the Item element.

If we were going to display the results of such a request in an MXML DataGrid, this poses a small problem since we can’t simply bind the datasource at the Item level nor the ItemAttributes level. To get around this, we’ll define the DataGrid with the “labelFunction” attribute that references a custom Action Script function that handles pulling data from the different levels.

First, the DataGrid Definition:

  1. <mx:DataGrid id=“dgSearchResponse” labelFunction=“responseLabels”
  2. dataProvider=“{itemSearchRequest.lastResult.ItemSearchResponse.Items.Item}”
  3. width=“626″
  4. >
  5. <mx:columns>
  6. <mx:DataGridColumn headerText=“Title” wordWrap=“true”/>
  7. <mx:DataGridColumn headerText=“Author” wordWrap=“true” />
  8. <mx:DataGridColumn headerText=“URL” wordWrap=“true” />
  9. </mx:columns>
  10. </mx:DataGrid>

Without the labelFunction, this DataGrid wouldn’t do much. With the labelFunction, we can parse the appropriate elements relative to the current Item element:


  1. <mx:Script><![CDATA[
  2. import mx.controls.dataGridClasses.DataGridColumn;
  3.  
  4. private function responseLabels(item:Object, column:DataGridColumn):String
  5. {
  6. var theLabel : String = “”
  7. var columnHeaderText : String = column.headerText;
  8. switch (columnHeaderText)
  9. {
  10. case “Title”:
  11. theLabel = item.ItemAttributes.Title;
  12. break;
  13. case “Author”:
  14. theLabel = item.ItemAttributes.Author;
  15. break;
  16. case “URL”:
  17. theLabel = item.DetailPageURL;
  18. break;
  19. }
  20. return theLabel;
  21. }
  22. ]]></mx:Script>

Here, notice that we simply put a switch statement together based on the current columnHeaderText value. For the Title and Author columns, we return the Title or Author child of the ItemAttributes element. For the URL, we return the DetailPageURL directly from the current Item.

Well, that’s pretty much it. In the next installment we’ll show how to use a custom event handler to link to the selected item’s DetailPageURL, as well as how to add some additional capabilities such as changing the sort order or searchIndex using a combo box.

You can see the working application here.

Pages: 1 2 3


Leave a Comment