Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using Amazon's Web Services Toolkit in Your Windows Forms Applications : Page 3

Amazon's free Web services let you query and display data from Amazon's Web site in your own applications. Despite the name, you're not limited to using Web services in Web applications. See how to use Amazon's new Web services in desktop applications, and see why Web services are becoming increasingly important in all applications.


advertisement
Returning Item Detail
That completes the first portion of the application. The second requirement is to get the detail information when a user clicks one of the titles. To do that, use the ListBox's SelectedIndexChanged event to retrieve the ASIN number of the selected item, and then perform an AsinSearchRequest to get the detail data.

// create a new AsinRequest object ar = new AsinRequest(); // get the ASIN number from the Details item stored // in the ListBox ar.asin = ((Details) listBox1.SelectedItem).Asin; // set properties ar.devtag=devtag; ar.type="heavy"; ar.tag = "webservices-20"; // perform the search pi = amazonSrch.AsinSearchRequest(ar); Details[] allDetails = pi.Details; // code to display detail here

This time, you want to perform a "heavy" search to return all the details. After retrieving the details, the sample code displays a selected subset by concatenating the information using a StringWriter, and then assigning it to the txtDetails TextBox on the form.



StringWriter sw = new StringWriter(); StringBuilder sb = new StringBuilder(); if ((allDetails != null) && (allDetails.Length > 0)) { Details d = allDetails[0]; sw.WriteLine("Title: " + d.ProductName ); sw.WriteLine("Asin: " + d.Asin); String[] authors = d.Authors; if (authors != null) { if (authors.Length > 1) { sw.Write("Authors: "); } else { sw.Write("Author: "); } for (int i=0; i < authors.Length; i++) { if (i>0) { sw.Write(", "); } sw.Write(authors[i]); } sw.WriteLine(""); } sw.WriteLine("Availability: " + d.Availability); sw.WriteLine("Browselist: " + d.BrowseList); sw.WriteLine("Catalog: " + d.Catalog); sw.WriteLine("CollectibleCount: " + d.CollectibleCount); sw.WriteLine("CollectibelPrice: " + d.CollectiblePrice); sw.WriteLine("Distributor: " + d.Distributor); sw.WriteLine("ImageUrlSmall: " + d.ImageUrlSmall); sw.WriteLine("ImageUrlMedium: " + d.ImageUrlMedium); sw.WriteLine("ImageUrlLarge: " + d.ImageUrlLarge); this.txtDetail.Text = sw.ToString(); sw.Close(); // more code here }

Displaying Images from a URL
The final task is to display the image. The ASIN request returns image URLs for large, medium, and small images, when available. The best size for the sample application is the medium image. Unfortunately, there's no simple method call to retrieve an image from a URL. Instead, you need to make a separate request to get the image data. The ImageFromURL() method in the sample application solves that problem.

// add a using System.Net reference to your class using System.Net; // retrieve an image from a URL private Image ImageFromUrl(String url) { WebClient wc = new WebClient(); Stream st = wc.OpenRead(url); Image im = Image.FromStream(st); st.Close(); return im; }

The ImageFromUrl method creates a WebClient instance and uses it to request the image data from the specified URL, assigns that to a Stream, and then loads the Image from the stream and returns it. You can assign the returned image directly to the PictureBox.Image property (see Figure 2):

// the variable d is the instance of the Details class filled with data returned // from the AsinSearchRequest method. this.pictureBox1.Image = this.ImageFromUrl(d.ImageUrlMedium);

Author's Note: The application only displays the medium-sized image, when it's available. Not all books have associated images—and of those that do, the image may not be available. Missing images don't cause an error, they simply don't display.
Figure 2. The sample application showing the details and an image for the item selected in the upper list.

This sample application is probably enough to get you started, but you can go far beyond this simple example and write applications that notify users when new books become available, monitor the sales rank of items on Amazon, retrieve Wish Lists and Marketplace information, and add transactional purchasing, including Amazon's "Quick-Click Buying" capability to your applications and Web pages.

You should be aware that there are some limitations attached to these free developer services. For example, as a developer, you're not allowed to make queries more than once per second, nor make queries containing more than 20K of data (which seems eminently reasonable). You agree to refresh cached information periodically; the refresh interval depends on the type of information. Read the Amazon.com_License.txt file carefully before you decide to use the services in your applications.



A. Russell Jones is the Executive Editor at DevX. Reach him at rjones@devx.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date