Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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.

Using the Amazon Web Services
After adding the Web Reference, using the services is straightforward. Add a using (C#) or an imports (VB.NET) statement to the top of your form, so you don't have to type the complete namespace string to access the classes.

C# using Amazon.com.amazon.soap; VB.NET Imports Amazon.com.amazon.soap

To make a SOAP query, you first create an instance of the AmazonSearchService class.

AmazonSearchService srch = new AmazonSearchService();

Next, create a class for the type of query you want to make.

Each query type has an associated class; for example, to perform a keyword search, create a new KeywordRequest instance; for the ASIN search, create an AsinRequest object. Next, you set the properties for the specific request. Finally, you pass the populated request to the appropriate method of the AmazonSearchService. For a keyword search, call the KeywordSearchRequest method. The names of the request classes and the AmazonSearchService's request methods match, so it's easy to match request types and request methods.

Most request types return a ProductInfo node in an XML document that looks similar to the KeywordSearchResponse document shown in Listing 1. The SOAP infrastructure handles mapping the returned SOAP elements to the classes provided by the proxy. For example, the <ProductInfo> element in the response contains response information, including a list of <Details> elements that contain the data for individual products. You create a ProductInfo class instance to retrieve the ProductInfo from the response, and then iterate through the list of Details to retrieve the item data for display.

KeywordRequest kr = new KeywordRequest(); kr.devtag="Your Developer Token here"; kr.keyword=this.txtSearch.Text; kr.mode="books"; kr.sort="+titlerank"; kr.tag="webservices-20"; kr.type="lite"; kr.page="1"; ProductInfo pi = srch.KeywordSearchRequest(kr); Details[] allDetails = pi.Details;

You must pass your Developer Token for each request. You can find valid values for the other parameters for the KeywordRequest (and all request types) in the documentation that accompanies the Amazon download, but the names are fairly intuitive. The example above searches for books containing the keywords the user entered in the txtSearch TextBox on the form, sorts the results alphabetically by title in ascending order, and returns the "lite" response type containing information for the first 10 results (kr.page="1").

Each query returns at most 10 items, so you need to be able to make multiple queries to get all the data for any particular keyword search. Each query that can return multiple values sets a ProductInfo.TotalResults property that you can use to determine whether the query has more possible results. For example, in the preceding example, a TotalResults value of 15 would mean that you could requery the Web service and request page 2, which would return the last five items:

if (pi.TotalResults > 10) // get the second page of results kr.page="2"; ProductInfo pi = srch.KeywordSearchRequest(kr); Details[] allDetails = pi.Details;

Displaying Query Results
The Details class exposes the detail fields for each item returned. It's a generic class that contains fields that apply to all items rather than only books, so not all queries populate all the fields. For example, here's a portion of the class as VS.NET initially creates it (see the reference.cs file in the sample code for the complete listing).

[System.Xml.Serialization.SoapTypeAttribute("Details", "urn:PI/DevCentral/SoapService")] public class Details { public string Url; public string Asin public string ProductName public string Catalog; public KeyPhrase[] KeyPhrases; public string[] Artists; public string[] Authors; public string Mpn; // more field definitions... }

In the sample code, you want to display the Details.ProductName field in a ListBox. Because the list of Details is simply an array of Details objects, you should be able to assign the array directly to the ListBox.DataSource property, and set the ListBox.DisplayMember property to the ProductName field. Unfortunately, that doesn't work. The DisplayMember property only works for methods, not for publicly exposed fields.

The simplest workaround is to change the public member fields to private variables and then add public property accessors for those variables to the value you want to display. For example, in the sample reference.cs file, you'll see that the Details class has been altered with the ProductName field implemented as a true property rather than a public member, as shown below.

public string ProductName { get { return mProductName; } set { mProductName = value; } }

After making that slight modification, you can set the ListBox properties to display the data.

ProductInfo pi = srch.KeywordSearchRequest(kr); Details[] allDetails = pi.Details; this.Cursor = Cursors.WaitCursor; this.listBox1.BeginUpdate(); this.listBox1.DataSource = null; this.listBox1.Items.Clear(); this.listBox1.DataSource=allDetails; this.listBox1.DisplayMember="ProductName"; this.listBox1.SelectedIndex=-1; this.listBox1.EndUpdate(); this.Cursor = Cursors.Default;

Author's Note: Depending on your needs, you may want to define a custom class to populate bound controls rather than altering the generated classes. Also, although the preceding code doesn't show error-trapping, watch out for errors—the Details array may be empty, which causes an error when you assign it to the ListBox.DataSource. Also, the KeywordSearchRequest can fail if the service is not available, or if the request is invalid.

Using the altered class, you can now perform a KeywordSearchRequest and display the results in a ListBox. There are two other minor problems. It can take a few seconds to get the data via the Web service. Because you don't want the user clicking on items during that time, let the user know that an action is in progress by changing the cursor and using the BeginUpdate/EndUpdate methods to disable the ListBox display for the duration of the search request and the update.

Comment and Contribute






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



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