站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Running the Amazon E-Commerce Service Sample Application In NetBeans

Running the Amazon E-Commerce Service Sample Application In NetBeans

摘要: Running the Amazon E-Commerce Service Sample Application In NetBeans

Amazon provides a rich set of web service interfaces that allow you do everything from querying Amazon's product data (Amazon E-Commerce Service) to executing your own programs on their computers (Amazon Elastic Compute Cloud). To help you understand how to use those web services, Amazon also provides comprehensive sample applications. In this article, I use the E-Commerice Service's sample Java application to show you how to build, run and debug the Amazon Web Services from the NetBeans IDE. By doing so you get all the NetBeans IDE benefits such as code completion, code hyper linking and integrated Javadoc assistance. Also, the ability to debug the application, single step though and monitor variables can go a long way to deepening your understanding of how the Amazon web services operate.

Setting Up Your Environment

Installing Java and NetBeans

The Amazon web services work best with J2SE 1.4.2. If you have neither J2SE 1.4.2 nor NetBeans installed, you can
Download
the NetBeans IDE + J2SE SDK Bundle.

If you just need NetBeans, download and install NetBeans.

If you just need J2SE 1.4.2, download and install the J2SE 1.4.2 SDK.

Amazon E-Commerce Services (ECS)

Register as an Amazon Web Services Developer (Free). Each Amazon web services request requires a Access Key ID parameter, which you'll receive by registering.

Download and extract the Java Sample for Amazon E-Commerce Service (download size: 2.7 MB). For the purposes of this article, we will assume the sample has been extracted to the root directory.

Creating the NetBeans Application

Create the NetBeans Application

  1. Start NetBeans.
  2. Choose File > New Project (Ctrl+Shift+N).
  3. Select Java Project with Existing Sources under the General Category.
  4. Name the project AmazonSample and choose a folder to host the project. Click Next.
  5. All of the Amazon sample application's Java sources are in the root of the extracted archive, ecs-javademo. Add this folder as a source package folder. Click Finish.

Add the Necessary JAR Files and Run the Application

  1. Right-click the Libraries node of the AmazonSample project and choose Add JAR/Folder....



  2. Browse to the ecs-javademo directory and select the following JARs (You can hold down the Ctrl key while you click to select multiple JARs at once):

    • axis.jar,
    • commons-discovery.java,
    • commons-logging.jar,
    • jaxrpc.jar and
    • saaj.jar.

  3. Right-click the Libraries node again and choose Properties...
  4. Set the Java Platform to 1.4.2 (If you don't see 1.4.2 in the list, click Manage Platforms and add the platform).



  5. Press F6 to run the project. You'll be prompted to select the main class, which is Main:



    And the you'll see the sample application:



    Note, if compilation fails with "as of release 1.5, 'enum' is a keyword, and may not be used as an identifier", make sure you set the Java Platform to 1.4.2 as described above.

Test the Application

The Amazon web services actually include a Help operation, which returns information on how to execute the other operations. For example, try the following:

  1. Select the SOAP tab of the sample application.
  2. Select Help from the combo-box.
  3. Enter the Access Key ID you received when you registered with Amazon in the SubscriptionId field.
  4. Enter ItemSearch in the About text field.
  5. Enter Operation in the HelpType text field.
  6. Click Send. You should see the following response (if you get a connect exception, see Setting Your Proxy below):


    Using this help feature, you can see the required parameter to ItemSearch is the SearchIndex. You also see that the default response group is Small, which means minimal information is returned (Note, the amazon web site also provides excellent API Reference documentation). With this information in hand, lets try the ItemSearch request:

  7. Select ItemSearch from the ComboBox
  8. Enter your Access Key ID into the SubscriptionId field.
  9. Enter an Author, such as Ludovic Champenois
  10. Enter ResponseGroup Medium.
  11. Enter the Search Index Books.
  12. Click Send.



Modifying the Sample Application

Setting Your Access Key ID

It would be nice if you didn't have to paste in your Amazon Access Key ID every time you wanted to run a different operation. Let's fix that:

  1. Open DataInput.java. Press Ctrl+7 (to open/switch to the Navigator), type "create" (notice the Quick Search field) and press Enter. You should now be at the createGUI method.
  2. Insert the code in blue between the two existing lines of code:
    commonTextFields[i] = new JTextField();
    if (commonParameterNames[i].equals("SubscriptionId")) {
        commonTextFields[i].setText("<your Access Key ID>");
    }
    container = new Container();
  3. Press F6 to test your changes.

Setting Your Proxy

If you're like me, behind a corporate firewall, you're probably experiencing the following exception:

Right-click the AmazonSample project and choose properties. Select the Run node and set the following VM Options:

-Dhttp.proxyHost=<your proxy host> -Dhttp.proxyPort=<your proxy port>

Here's an example from my machine:

Debugging the Application

One of the real advantages to running the sample application from NetBeans is the ability to debug it.

  1. Open DataInput.java. Press Ctrl+7, type "create"and press Enter to navigate to the createGUI method.
  2. Click the left margin of the editor to set a breakpoint on the line:
    commonTextFields[i] = new JTextField();


  3. Press F5 to debug the project. Program execution will halt on your breakpoint:



  4. Press Alt+Shift+1 to open the Local Variables window
  5. Expand this > commonParameterNames. This is how I knew to test for parameter name equals "SubscriptionId".



  6. Press F8 (to step over) or F7 (to step into) the code. Experiment and have fun.

 


↑返回目录
前一篇: Reverse Engineering Java Applications
后一篇: Securing a Web Application in NetBeans IDE 5.5