当前页面: 开发资料首页 → Netbeans 专题 → Using NetBeans to Develop with the eBay SDK for Java
摘要: This tutorial demonstrates how to get up and running with the eBay SDK for Java. By using NetBeans to develop your eBay applications, you'll have the added productivity of code completion, Javadoc help, code compilation, execution and debugging all from inside the NetBeans IDE.
This tutorial demonstrates how to get up and running with the eBay SDK for Java. By using NetBeans to develop your eBay applications, you'll have the added productivity of code completion, Javadoc help, code compilation, execution and debugging all from inside the NetBeans IDE.
I'll be using the Console View sample application provided with the SDK, however, the steps involved would apply to all of the eBay sample applications, or to creating your own application from scratch. The sample applications provided with the eBay SDK already have Ant scripts associated with them, so I will be using the option to create a NetBeans project with existing Ant script. I also try to keep the alterations of the existing Ant script to a minimum, so I'll do things like set up the JAVA_HOME environment variable, because the script is looking for it. In the end, you'll be able to run this script inside the IDE and out, which is one of the powerful features of NetBeans. Let's get started.
Installing Java and NetBeans
The latest versions of the eBay SDK now work with Java SE 5.0. If you have neither Java SE 5.0 nor NetBeans 5.5 installed, you can download the NetBeans IDE + J2SE SDK Bundle.
If you just need NetBeans, download and install NetBeans 5.5.
If you just need Java SE 5.0, download and install the Java SE 5.0 SDK.
Obtaining and preparing to use the eBay SDK for Java
Join the eBay Developers Program. This is necessary in order to test your application against the eBay sandbox.
For this tutorial, you need to register at least one test user in the sandbox. For more thorough testing, you'll need at least 2 test users, one to sell and one to buy.
Create a single-user authentication token for your test user(s). This token is used to authenticate and authorize yourself from with-in your application to the eBay server.
Download the and install the eBay SDK for Java. At the time of the last article update, Java SDK v485 point was used (whatever version you choose, be sure it supports JDK 1.5). For this exercise, we will assume it has been extracted to the root folder.
Set the environment variable JAVA_HOME to the location where you installed Java SE 5.0. For example C:\Program Files\Java\jdk1.5.0_06.
Add the eBay SDK Class Libraries to NetBeans. This will provide the support for Javadoc and view sources.
Select Library Manager from the Tools Menu.
Click the New Library... button. Give the library a name, such as eBay. Leave the Type as Class Libraries and select OK.
With eBay and the Classpath tab selected, click the Add JAR/Folder button and select all the JAR files in the <eBay SDK Install Location>/lib/ directory. These should be attributes.jar, ebaycalls,jar, ebaysdkcore.jar, eps.jar and helper.jar.
Switch to the Sources tab, click the Add JAR/Folder button again, and add the following folders:
Creating a New Project
Choose File > New Project (Ctrl+Shift-N).
Select Java Project with Existing Ant Script under the General Category. This will create what's known as a Free-Form Project in NetBeans. To learn more about this project type, see Advanced Free-Form Project Configuration.
For the Location, browse to the samples/consoleViewItem folder where you installed the eBay SDK. The wizard will locate the build script and recommend a Project Name. Select Next.
Build and Run Actions. The wizard located the Build, Clean and Javadoc targets. Unfortunately, there is no run target, so we'll have to create that later. Select Next.
Add the application's source package folder, which is simply src.
Click Finish.
Build the Project
At this point, NetBeans is able to build your application. Select Build | Build Main Project, press F11, or right-click the project node and choose Build Project from the context menu. You'll see the output from the Ant script in the Output in the bottom half of the IDE.
Editing the Application
The Console View sample application requests lots of user input. We'll replace these calls with hard coded values, making the application easier to test.
Expand the source folder and open the ApplicationViewItem.java. The first thing you may notice is the red badges in the left margin which indicate compiler errors. The project compiled correctly in the previous step because the Ant script knows about all of the project's dependancies. Know we're going to let the IDE know about the project's dependancies.
Add the eBay SDK JARs to the project's classpath:
Right-click the consoleViewItem project node and select properties.
Select Java Sources Classpath and choose Add JAR/Folder
Select all the JAR files in the <eBay SDK Install Location>/lib/ directory. These should be attributes.jar, ebaycalls,jar, ebaysdkcore.jar, eps.jar and helper.jar.
Select OK. NetBeans will parse the JAR files and you'll see the compiler badges disappear from ApplicationViewItem.java.
To the top of the main method, add your authentication and authorization token, eBay SOAP server URL, and item ID to view:
22 public static void main(String[] args) { 23 24 String token = "<Enter your token>"; 25 String eBaySOAPURL = "https://api.sandbox.ebay.com/wsapi"; 26 String itemIDStr = "<enter id of item to view>";Get any item id from http://sandbox.ebay.com. At the time of this writing, I used 110011834924.
47 input = ConsoleUtil.readString("Enter your eBay Authentication Token: ");
45 cred.seteBayToken(token);
48 input = ConsoleUtil.readString("Enter eBay SOAP server URL (e.g., https://api.ebay.com/wsapi): ");
48 apiContext.setApiServerUrl(eBaySOAPURL);
56 String itemIDStr = ConsoleUtil.readString("Enter ID of the item that you want to get:"
Press F11 to compile the project.
Add a run target to the build.xml
Open build.xml
Scroll to the bottom of the file and add the following target:
105 <target name="run" depends="compile" description="Run Sample"> 106 <echo message="Running"/> 107 <java classname="consoleviewitem.ApplicationViewItem" fork="yes" failonerror="false" dir="./"> 108 109 <classpath refid="project.class.path"/> 110 <classpath> 111 <pathelement location="${dest}"/> 112 </classpath> 113 </java>
Map the target
Open the Project Properties and select Build and Run. Set the Run Project to the new run target we just added.
Run the Application
Right click the consoleViewItem project and choose Run Project.
If all goes well, you should see the item information for the
selected auction ID in the output window. In my experience, the
errors returned by eBay are pretty clear, so debugging should not
be a problem.
Note, if you are behind a corporate proxy and get a ConnectException, you can add the following two lines of code to the top of ApplicationViewItem's main method:
22 public static void main(String[] args) { 23 24 System.setProperty("http.proxyHost", "<your proxy host>"); 25 System.setProperty("http.proxyPort", "<your proxy port>");
The NetBeans application debugger is another benefit to using the NetBeans IDE to create your eBay applications. As your application grows, you can use the debugger help solve logic problems.
Set the Output Folder
Open the project properties and select the Output category.
8 <nbjpdastart addressproperty="jpda.address" name="consoleViewItem.jpx" transport="dt_socket"> 9 <sourcepath path="${src}"/> 10 </nbjpdastart>
Set a breakpoint and Debug the Application.
Click on the left margin on the ApplicationViewItem.java to set a breakpoint.
Press F5 to Debug the Project.
From here you can step though your application, viewing local variables, setting watches, etc.