29.4.09

Google Calendar API

ความสามารถของ Google Calendar มีประโยชน์มากมายอย่างที่ทราบกันดี ทาง Google ก็เปิดช่องทางให้เราสามารถเข้าถึงบริการของเค้าได้ง่าย
จึงขอลอง เพื่อเอามาประยุกต์ใช้กับโปรเจคใหม่ที่กำลังจะเกิดขึ้น
ตัวอย่างนี้จะทำการสร้าง event และส่งเข้า Google Calendar ของเรา

import java.net.URL;
import java.util.Calendar;
import util.Constant;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.extensions.Reminder;
import com.google.gdata.data.extensions.Reminder.Method;
import com.google.gdata.util.AuthenticationException;


public class GoogleCalendarUtil {

public CalendarService authenticate(String username, String password) {
CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
try {
System.out.println("authenticate: start");
myService.setUserCredentials(username, password);
System.out.println("authenticate: success");
} catch (AuthenticationException e) {
e.printStackTrace();
}
return myService;
}

public void createEvent(CalendarService service, String content, String username) {
try {

URL postURL = new URL("http://www.google.com/calendar/feeds/" + username + "/private/full");

Calendar calc = Calendar.getInstance();
calc.add(Calendar.MINUTE, 1);
String timeStartEvent = calc.get(Calendar.HOUR_OF_DAY) + ":" + calc.get(Calendar.MINUTE);
// calc.add(Calendar.MINUTE, 1);
String timeEndEvent = calc.get(Calendar.HOUR_OF_DAY) + ":" + calc.get(Calendar.MINUTE);

System.out.println("addEvent: start");
CalendarEventEntry myEntry = new CalendarEventEntry();
content += " " + timeStartEvent + "-" + timeEndEvent;
System.out.println("content: " + content.toString());
myEntry.setContent(new PlainTextConstruct(content.toString()));
myEntry.setQuickAdd(true);

CalendarEventEntry insertedEntry = service.insert(postURL, myEntry);

Reminder reminder = insertedEntry.getReminder().get(0);
reminder.setMinutes(0);
reminder.setMethod(Method.SMS);
insertedEntry.getReminder().set(0, reminder);
insertedEntry.update();
System.out.println("addEvent: complete");
} catch (Exception e){
e.printStackTrace();
}
}

public static void main(String[] args) {
String username = "username";
String password = "password";
String content = "hello";
GoogleCalendarUtil helloGoogle = new GoogleCalendarUtil();
CalendarService service = helloGoogle.authenticate(username, password);
helloGoogle.createEvent(service, content, username);
}


}

เมื่อทำงานเสร็จคุณจะได้ sms แจ้ง event ที่ใส่เข้าไป
แน่นอนตรงจุดนี้ทำให้เราสามารถนำไปประยุกต์กับบริการต่่างๆ ได้อีกเยอะ

No comments:

Post a Comment