Coding in the Shade: Using Eclipse with Google Data APIs

Introduction

Eclipse is a very handy (and free!) IDE that has a special place in the hearts of many Java programmers. It is not a surprise then, that you might want to use it with the Java client library to make a killer Java application that works with one of the Google Data APIs.

Setting up Eclipse

If you don't have Eclipse installed yet, you've been missing out. Eclipse can make Java development a lot easier, due to its ability to suggest fixes, find missing imports, and autocomplete methods. Go to the Eclipse.org downloads page to get the installer for your operating system. To run Eclipse you'll have to have a JRE installed, which is linked to from the Eclipse downloads page. Once you've installed Eclipse, you'll need to create a workspace. These examples use /usr/local/eclipse/workspace.

Installing required libraries

Eclipse provides a lot of Java functionality out of the box, so this should be easy. First things first, grab the latest version of the Java client library from the project downloads page. Note that there are two separate downloads: one for samples, and one for the source code. Download the samples zip, since that includes the compiled JAR files of the Java client library needed to create your own programs. If you are curious about how the client library itself works, you can download the source zip and look through it, but that is outside the scope of this article. Once you've downloaded the samples zip, unzip the file to a convenient directory where you can find the files. The examples use /usr/local/eclipse. This means the JAR files for the project should now be accessible under /usr/local/eclipse/gdata/java/lib.

Now that you have installed the client library, the next thing to do is download some external dependencies so we can include them in the project build path. The INSTALL-samples.txt file contains a list of jars you'll need and locations to download them from. The current list is:

FilenameLocation
mail.jarSun's JavaMail API
activation.jarSun's JavaBeans Activation Framework
servlet-api.jarApache Tomcat

In this example, these JAR files are downloaded and copied to /usr/local/eclipse/gdata/third_party.

Whew! Now you should have all the pieces you need to start developing a Java application that uses one of the Google Data APIs. Read on!

Creating a new application

The first step is to open Eclipse and select "New > Java Project" from the File menu.

 

Now click Next and select the Libraries tab. Add in all of the dependent JAR files you downloaded by clicking on Add External JARs.

Adding dependency JARs

You also will have to add in the JAR files from the Java client library. They are located in the java/lib subdirectory of where you extracted the client library. You will always have to include the following JAR files: gdata-client, gdata-client-meta, gdata-core, and gdata-media.

Adding GData JARs

Now choose which API you wish to use. This example uses the Google Documents List API. This requires adding in the appropriate JAR files: gdata-docs and gdata-docs-meta.

Adding DocList JARs

Finally you can click "Finish" and create your new project.

The newly created project

To make a simple test application, create a new class with a main method by selecting "New > Class" from the File menu.

Creating a class

Now I'm going to paste in some code from the Developer's Guide for the Documents List API.

Sample code with errors

You will see a lot of errors until you add the appropriate import statements. Eclipse makes this easy, all you have to do is click on the error markers on the left to get a helpful menu that lets you add imports.

The quickfix menu

Once you have cleaned up the imports, you also have to handle exceptions that can be thrown by our code. The example opts to surround the entire sample code with a very rudimentary exception handler.

Working sample code

Now, assuming that you substitute the username and password with a valid test account you have created, you should be able to see a list of documents owned by that test account when you choose "Run" from the Run menu.

The program output

Hooray!

Conclusion

Now that you have a working instance of Eclipse and all the required JAR files in place, you are well on your way to building an awesome Java application that works with one of the Google Data APIs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值