Wednesday, November 21, 2007

Google Base Data API -- Java

下面来看看如何使用Java开发库吧。来自http://code.google.com/support/bin/answer.py?answer=78455&topic=11983
 
1. 环境安装
  • JDK (Java Development Kit) version 1.5+
  • Apache Ant version 1.7+
  • mail.jar in Sun's JavaMail API 1.4+
  • activation.jar in Sun's JavaBeansActivationFramewrok. This is only required for media specific APIs including Document List Data API, Picasa Web Album API, and YouTube Data API.
  • servlet.jar in Sun's Servlet API version 2.3+. This is required only if executing code samples in 'sample.authsub' or 'sample.gbase.recipe' packages.
。。嘿嘿不翻译了,下载是基于Windows的

Installing Java

The Java development package is available from Sun in the form of the JDK (Java Development Kit). The JRE (Java Runtime Environment) and everything else you need to write, compile, test, and debug Java applications are contained within. If you already have Java installed, skip ahead to installing Ant.

Note: You need to have administrative permissions to install the JDK.

  1. Visit http://java.sun.com/javase/downloads/index.jsp
  2. Click "Download" next to the latest update.
  3. Read and accept the License Agreement.
  4. Download one of the Windows Installation files.
  5. Run the installer.
  6. Edit the Path environment variable to easily run JDK executables like javac. The path you'll want to add will look something like C:\jdk\bin\. See the Appendix for instructions on how to edit the Path variable in Windows.

If you have any problems installing the JDK, check out the detailed installation instructions on Sun's website.

Installing Ant

Apache Ant is a build tool for Java similar to make (but better). You'll be using it to build the library and execute samples. If you already have Ant installed, check that it's version 1.7 or higher.

  1. Click the "Start" menu, and then "Run."
  2. Type cmd into the text field.
  3. Type ant -version into the prompt to see which version you have.

If you have 1.7+, skip ahead to getting mail.jar. If your version of Ant is lower than 1.7 or you don't have it installed, follow the steps below.

  1. Visit http://ant.apache.org/bindownload.cgi
  2. Click on the download link for the .zip download.
  3. Extract all files into the location of your choice, like C:\ant
  4. Add the ant bin directory, like C:\ant\bin to your Path environment variable.
  5. Add or edit the ANT_HOME environment variable to set it as the new location, e.g. C:\ant
  6. Add or edit the JAVA_HOME environment variable to where Java is installed, e.g. C:\jdk.

You can find detailed installation instructions at Ant's website if you run into any issues.

Getting mail.jar

The JavaMail API is included for media support.

  1. Visit http://java.sun.com/products/javamail/downloads/index.html
  2. Click the "Download" button. Ensure that you are downloading version 1.4 or greater.
  3. Read and accept the License Agreement.
  4. Click the link for the .zip file containing JavaMail.
  5. Extract the files to your computer and note the location. mail.jar will be under the root directory, javamail-(version)

Getting activation.jar

The JavaBeans Activation Framework is included for media support.

  1. Visit http://java.sun.com/products/javabeans/jaf/downloads/index.html
  2. Click the "Download" button.
  3. Read and accept the License Agreement.
  4. Click the link for the .zip file containing the Activation Framework.
  5. Extract the files to your computer and note the location. activation.jar will be under the root directory, jaf-(version)

Getting servlet.jar

servlet.jar (it may be called servlet-api.jar) can be found in your servlet container (J2EE, Tomcat, WebLogic, etc.). It is used for the AuthSub and Google Base samples. The following instructions go through getting Tomcat 6.x.

  1. Visit http://tomcat.apache.org/download-60.cgi
  2. Click on the Windows Service Installer download under Core Binary Distributions.
  3. Run the installer.
  4. If you stayed with the default, servlet-api.jar should be located at C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
 

Installing the Google Data Client Library

  1. Visit http://code.google.com/p/gdata-java-client/downloads/list
  2. Click on the featured download.
  3. Extract the file onto your computer.
  4. Navigate to gdata/java/build/build.properties and open the file.
  5. Edit the external dependencies to point to the locations of the .jar files on your local machine.
  6. Note: On Windows, be sure to escape the backslashes. For example,

    servlet.jar=C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\lib\\servlet-api.jar
     
我的做法: 拷贝mail-1.4.jar,servlet-api.jar,activation-1.1.jar到gdata\java\lib,然后对应更改build/build.properties,如下
servlet.jar=./lib/servlet-api.jar
mail.jar=./lib/mail-1.4.jar
activation.jar=./lib/activation-1.1.jar

Running Samples

All of the available samples are located under gdata/java/sample. The gdata/java/build/build.properties file contains all the input values for the samples contained in the library. Set sample.credentials.username and sample.credentials.password to a valid username and password. We can use Ant to build as well as execute the samples.

Test to see if you've installed everything correctly by opening a command prompt, changing to the gdata/java directory, and typing:

ant sample.calendar.run

You may receive some info or warning messages, but just look for the BUILD SUCCESSFUL message at the end! See the troubleshooting section if you don't get a success message.

Try out a more interactive sample by typing:

ant sample.spreadsheet.guidemo.run

To find out how to run a particular sample, go to gdata/java/build and check the build file for that sample. Look for the samples run section.

Troubleshooting

If your build fails with an error message like,

BUILD FAILED Target 'core.sample.core.util.build' does not exist in this project. It is used from target 'sample.calendar.build'.  Total time: 0 seconds 

or a similar error message about an essential file missing from the project, you may be running an older version of Ant. Type ant -version to make sure you're running 1.7 or higher. Refer to the dependency instructions above to get the latest version of Ant.

Building Your Own Applications

The next question is how to build your own application. I'll go through a "Hello, World!" equivalent program using the Calendar service to exhibit basic functionality. More detailed information can be found in the Java Client Library's developer guide, as well as the individual product developer guides.

Create a file called CalendarTest.java. Start by including the following import statements.

import com.google.gdata.client.*; import com.google.gdata.client.calendar.*; import com.google.gdata.data.*; import com.google.gdata.data.acl.*; import com.google.gdata.data.calendar.*; import com.google.gdata.data.extensions.*; import com.google.gdata.util.*;  import java.net.*; import java.io.*;  import sample.util.*; 

Here is the whole program (without any exception handling).

public class CalendarTest { 	     public static void main(String[] args) {         CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0");         myService.setUserCredentials("root@gmail.com", "fortytwo");                      URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");         CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);                      System.out.println("Your calendars:");         System.out.println();                  for (int i = 0; i < resultFeed.getEntries().size(); i++) {           CalendarEntry entry = resultFeed.getEntries().get(i);           System.out.println("\t" + entry.getTitle().getPlainText());         } 		         } }

This little program will request all the calendars you own and display all the titles. It's a little longer than the canonical "Hello, World!" example, but it's very simple once we break it down. The first couple of lines creates a service object and sets the user credentials.

CalendarService myService = new CalendarService("exampleCo-exampleApp-1.0"); myService.setUserCredentials("root@gmail.com", "fortytwo");

Then, the URL of the resource is set. In this case, this is where you can request the list of all calendars from the authenticated user.

URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");

The line below will execute the actual GET command on the URL and put the resultant feed into a tidy object.

CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);

The for loop below will iterate through each entry and print the title. Note that the title is stored as a TextConstruct, so an extra function call is required to get the plain text.

for (int i = 0; i < resultFeed.getEntries().size(); i++) {           CalendarEntry entry = resultFeed.getEntries().get(i);           System.out.println("\t" + entry.getTitle().getPlainText()); 	}

That was pretty basic — let's go through a few other common things. The following snippet will show you how to create an object and insert it. For our example, it will be a new calendar event entry.

URL postURL = new URL("http://www.google.com/calendar/feeds/gdata.ops.demo@gmail.com/private/full"); CalendarEventEntry myEvent = new CalendarEventEntry();  //Set the title and description myEvent.setTitle(new PlainTextConstruct("Pi Day Party")); myEvent.setContent(new PlainTextConstruct("I am throwing a Pi Day Party!"));  //Create DateTime events and create a When object to hold them, then add  //the When event to the event DateTime startTime = DateTime.parseDateTime("2007-03-14T15:00:00-08:00"); DateTime endTime = DateTime.parseDateTime("2007-03-14T17:00:00-08:00"); When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); myEvent.addTime(eventTimes);  // POST the request and receive the response: CalendarEventEntry insertedEntry = myService.insert(postURL, myEvent); 

Another common operation is building a query.

//Create a new query object and set the parameters Query myQuery = new Query(feedURL); myQuery.setFullTextQuery("Pi"); 		     //Send the request with the built query URL CalendarEventFeed myResultsFeed = myService.query(myQuery, CalendarEventFeed.class);  //Take the first match and print the title if (myResultsFeed.getEntries().size() > 0) {     CalendarEventEntry firstMatchEntry = new CalendarEventEntry();     myResultsFeed.getEntries().get(0);      System.out.println(firstMatchEntry.getTitle().getPlainText()); }

While debugging, another useful operation is dumping out the raw XML. There's a handy utility that you can use to do this in the library. Make sure samples.util.* is imported. Then, dump the feed or entry.

CommonUtils.dump(resultFeed, System.out);

For even deeper debugging tools, check out this article about how to turn on logging from within the client library.

This should give you a feel for what building apps using the client library is like. For more detailed information, see the developer guides.

Tuesday, November 20, 2007

Google Base Data API -- Using the Base Data API

简单介绍如何使用Google Base Data API,我现在只记得Java了。。。
 
1. 客户端开发库
   Google 目前支持Python、PHP、Java、.Net、Object-C语言进行开发。
 
2. 开发社区Group
 
3. Debug
  参考下面的内容可以帮助你调试你的应用。
 
4. Java开发库
    Java开发库支持的Google产品:
  • Calendar
  • Spreadsheets
  • Google Base
  • Picasa Web Albums
  • YouTube
  • Google Apps Provisioning
  • Codesearch
  • Notebook
 
 

Google Base Getting Start

本文内容来自
 
 
Google Base Data API的简单介绍
 
1. 如何开始
 
2. 什么是Google Base Data API?
  Google提供的一种API协议,来同Google Base Server进行交互。
  允许在Google Base Server上对Item进行插入,更新,删除操作。
 
  通过Google Base Data API编程,能够把应用整合集成到Google Base。
  如何去做,参考http://code.google.com/apis/base/
 
3. 谁应该使用Google Base Data API
   例子网站
  当然还有Google Base
    http://base.google.com/
  如果你希望构建一个公开无私的应用,请考虑使用Google Base Data API。
  当然你需要了解HTTP协议。
 
4. 我能限制对Item的访问吗?
   不能。Google Base所有的内容都是公开的,可以被所有人搜索到。
   但是,只有你可以通过/feeds/items进行编辑。
 
5. 使用API需要一个Google Base帐号吗?
   如果只是搜索,不需要Google Base帐号。
   但是需要去下面地址申请一个开发Key。
 
   如果要Insert/Delete/Query的话,需要认证你的Google Base帐号。
   你可以使用AuthSub(对于Web系统)获取的认证Token或帐号密码来进行认证。
   下面的地址可以获取一个Google Base帐号。
 
6. 如何对结果进行排序
   缺省,API自动根据relevancy进行排序。
   可以使用orderby参数指定排序方式。
 
7. 是否支持增量变更
   支持的。 只需要提交更新版本的Item,根据需要更改属性和值。
 
8. 如何进行查询?
   Google Base Data API使用HTTP Get方式进行查询。
   你可以通过查询公开feed,添加搜索参数进行查询。
    http://code.google.com/apis/base/query-lang-spec.html
   成功的查询结果通过Atom feed进行返回。
   另外,必须使用开发Key来进行查询。
 
9. 基于位置查询
   使用"+ radius"可以对针对位置进行查询。
   例: [location: @"1600 Amphitheatre Parkway, Mountain View, CA, USA" + 100mi] .
 
10. 基于国别查询  
   通过如下例子可以指定国别和语言来进行查询。
   <g:target_country>DE</g:target_country>
   <g:item_language>DE</g:item_language>

Thursday, February 22, 2007

日本語の漢字データベースを見つけ出した

今日は、日本語の漢字データベースを見つけ出した。

以下のURLを見て、役に立つと思う。

日本語漢字DBのウェブサイト、MYSQL、PHPで作った。しかし、ソースを見れません、残念だね。
http://www.whiteknightlogic.net/kanjidb/

WWWJDIC 紹介
http://www.csse.monash.edu.au/~jwb/wwwjdicinf.html
KANJIDIC/KANJD212
http://www.csse.monash.edu.au/~jwb/kanjidic.html
http://www.csse.monash.edu.au/~jwb/kanjidic2/
EDICT
http://www.csse.monash.edu.au/~jwb/edict.html


日辞郎
http://www.eijiro.jp/

研究したいんですが、もしできれば、GTRobotに機能を増えよう。

Tuesday, February 20, 2007

GTRobot's blog

This blog is for GTRobot project's publishing.

See you soon.