go to http://oracle.in.th

Thursday, November 19, 2009

การใช้งาน Google Contact Data API

Google มี API ให้เราสามารถนำมาใช้ในการติดต่อกับ Application ต่างๆของ Google ได้ เช่น Google Contact, Google Calendar การใช้ Contact Data API ทำได้ไม่อยาก โดยสามารถทำตามได้จากวิธีการดังนี้

1. ตรวจสอบว่าในเครื่องมีการติดตั้ง Eclipse และ Java Compiler ไว้เรียบร้อยแล้ว

2. Google Contact จะสามารถดึงข้อมูลจาก Google Account โดยที่จะต้องมี Account ของ Google ก่อน

3. จากนั้นให้ดาวน์โหลด Java Client Library จาก http://code.google.com/p/gdata-java-client/ ให้ดาวน์โหลดไฟล์ ชื่อ "gdata-samples.java-1.39.0.zip" มาทำการแตกไฟล์ไว้ใน "/eclipse/gdata"

4. จากนั้นให้ทดสอบการใช้ API โดยการทดลองรันโปรแกรมตัวอย่าง จาก "data/java/sample/contacts/ContactsExample.java"

5. โดยการ Compile โปรแกรมตัวอย่างจำเป็นต้อง import library ที่จำเป็นดังต่อไปนี้
import com.google.gdata.client.*;

import com.google.gdata.client.contacts.*;

import com.google.gdata.data.*;

import com.google.gdata.data.contacts.*;

import com.google.gdata.data.extensions.*;

import com.google.gdata.util.*;

import java.io.IOException;

import java.net.URL;
6. หลังจากทดสอบด้วยโปรแกรมตัวอย่างแล้ว ต่อไปจะเป็นขั้นตอนการใช้ Contact API ในขั้นตอนแรกเราจะต้องทำการเซ็ต
ContactsService myService = new ContactsService("exampleCo-exampleApp-1");

myService.setUserCredentials("your_account@gmail.com", "password");
7. ต่อไปเป็นการจัดการกับ Google Contact ที่สามารถทำได้ คือ Add New Contact, Update Contact และการ Delete Contact อันดับแรกคือ การ Add new Contact สามารถทำตามจาก source code ต่อไปนี้
public static ContactEntry createContact(ContactsService myService,

String fullName, String givenName, String familyName, String notes)

throws ServiceException, IOException {

// Create the entry to insert

ContactEntry contact = new ContactEntry();

Name name = new Name();

final String NO_YOMI = null;

 name.setFullName(new FullName(fullName, NO_YOMI));

 name.setGivenName(new GivenName(givenName, NO_YOMI));

name.setFamilyName(new FamilyName(familyName, NO_YOMI))

contact.setName(name);

contact.setContent(new PlainTextConstruct(notes));

 

Email primaryMail = new Email();

primaryMail.setAddress("liz@gmail.com");

primaryMail.setRel("http://schemas.google.com/g/2005#home");

 primaryMail.setPrimary(true);

contact.addEmailAddress(primaryMail);

 

Email secondaryMail = new Email();

secondaryMail.setAddress("liz@example.com");

 secondaryMail.setRel("http://schemas.google.com/g/2005#work");

secondaryMail.setPrimary(false);

contact.addEmailAddress(secondaryMail);

 

ExtendedProperty favouriteFlower = new ExtendedProperty();

favouriteFlower.setName("favourite flower");

favouriteFlower.setValue("daisy");

 contact.addExtendedProperty(favouriteFlower);

 

ExtendedProperty sportsProperty = new ExtendedProperty();

sportsProperty.setName("sports");

XmlBlob sportKinds = new XmlBlob();

sportKinds.setBlob(new String(""));

sportsProperty.setXmlBlob(sportKinds);

contact.addExtendedProperty(sportsProperty);

 

// Ask the service to insert the new entry

URL postUrl = new URL("http://www.google.com/m8/feeds/contacts/liz@gmail.com/full");

return myService.insert(postUrl, contact);

}
8. ต่อไปคือการ Update Contact โดยการที่จะ Update Contact นั้นจำเป็นที่จะต้องเรียก Contact ทั้งหมดออกมาเพื่อนที่จะรู้ว่าจะสามารถ Update Contact ใดได้บ้าง โดยการเรียก Contact ทั้งหมดออกมาสามารถทำได้จากการใช้ source code ดังต่อไปนี้
public static void printAllContacts(ContactsService myService)

throws ServiceException, IOException {

// Request the feed

URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/liz@gmail.com/full");

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

// Print the results

System.out.println(resultFeed.getTitle().getPlainText());

for (int i = 0; i < resultFeed.getEntries().size(); i++) {

ContactEntry entry = resultFeed.getEntries().get(i);

if (entry.hasName()) {

Name name = entry.getName();

if (name.hasFullName()) {

String fullNameToDisplay = name.getFullName().getValue();

        if (name.getFullName().hasYomi()) {

          fullNameToDisplay += " (" + name.getFullName().getYomi() + ")";

        }

        System.out.println("\t\t" + fullNameToDisplay);

      } else {

        System.out.println("\t\t (no full name found)");

      }

      if (name.hasNamePrefix()) {

        System.out.println("\t\t" + name.getNamePrefix().getValue());

      } else {

        System.out.println("\t\t (no name prefix found)");

      }

      if (name.hasGivenName()) {

        String givenNameToDisplay = name.getGivenName().getValue();

        if (name.getGivenName().hasYomi()) {

          givenNameToDisplay += " (" + name.getGivenName().getYomi() + ")";

        }

        System.out.println("\t\t" + givenNameToDisplay);

      } else {

        System.out.println("\t\t (no given name found)");

      }

      if (name.hasAdditionalName()) {

        String additionalNameToDisplay = name.getAdditionalName().getValue();

        if (name.getAdditionalName().hasYomi()) {

          additionalNameToDisplay += " (" + name.getAdditionalName().getYomi() + ")";

        }

        System.out.println("\t\t" + additionalNameToDisplay);

      } else {

        System.out.println("\t\t (no additional name found)");

      }

      if (name.hasFamilyName()) {

        String familyNameToDisplay = name.getFamilyName().getValue();

        if (name.getFamilyName().hasYomi()) {

         familyNameToDisplay += " (" + name.getFamilyName().getYomi() + ")";

        }

        System.out.println("\t\t" + familyNameToDisplay);

      } else {

        System.out.println("\t\t (no family name found)");

      }

      if (name.hasNameSuffix()) {

        System.out.println("\t\t" + name.getNameSuffix().getValue());

      } else {

        System.out.println("\t\t (no name suffix found)");

      }

    } else {

      System.out.println("\t (no name found)");

    }

 

    System.out.println("Email addresses:");

    for (Email email : entry.getEmailAddresses()) {

      System.out.print(" " + email.getAddress());

      if (email.getRel() != null) {

        System.out.print(" rel:" + email.getRel());

      }

      if (email.getLabel() != null) {

        System.out.print(" label:" + email.getLabel());

      }

      if (email.getPrimary()) {

        System.out.print(" (primary) ");

      }

      System.out.print("\n");

    }

 

    System.out.println("IM addresses:");

    for (Im im : entry.getImAddresses()) {

      System.out.print(" " + im.getAddress());

      if (im.getLabel() != null) {

        System.out.print(" label:" + im.getLabel());

      }

      if (im.getRel() != null) {

        System.out.print(" rel:" + im.getRel());

      }

      if (im.getProtocol() != null) {

        System.out.print(" protocol:" + im.getProtocol());

      }

      if (im.getPrimary()) {

        System.out.print(" (primary) ");

      }

      System.out.print("\n");

    }

 

    System.out.println("Groups:");

    for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) {      

      String groupHref = group.getHref();

      System.out.println("  Id: " + groupHref);

    }

 

    System.out.println("Extended Properties:");

    for (ExtendedProperty property : entry.getExtendedProperties()) {

      if (property.getValue() != null) {

        System.out.println("  " + property.getName() + "(value) = " + 

            property.getValue());

      } else if (property.getXmlBlob() != null) {

        System.out.println("  " + property.getName() + "(xmlBlob)= " + 

            property.getXmlBlob().getBlob());

     }

    }

 

    String photoLink = entry.getContactPhotoLink().getHref();

    System.out.println("Photo Link: " + photoLink);

 

    if (photoLink.getEtag() != null) {

      System.out.println("Contact Photo's ETag: " + photoLink.getEtag());

    }

 

    System.out.println("Contact's ETag: " + entry.getEtag());

  }

}
9. หลังจากที่รับทุก Contact มาแล้วจากนั้นจำเป็นการ Update Contact ที่ต้องการโดยใช้ Source code ต่อไปนี้ โดยในตัวอย่างนี้จะเป็นการ Update เฉพาะ ชื่อของ Contact นั้นเท่านั้น
public static ContactEntry updateContactName(

    ContactsService myService, ContactEntry entryToUpdate, String newName)

    throws ServiceException, IOException {

  entryToUpdate.setTitle(new PlainTextConstruct(newName));

  URL editUrl = new URL(entryToUpdate.getEditLink().getHref());

  return myService.update(editUrl, entryToUpdate);

}
10. และสุดท้ายคือการลบ Contact ซึ่งการจะลบ Contact จะต้องรู้ว่าต้องการลบ Contact ใด แล้วจึงทำการลบ Contact ออกจาก Account โดยใช้ Source Code ดังต่อไปนี้
public static void deleteContact(ContactEntry entryToDelete)

    throws ServiceException, IOException {

 entryToDelete.delete();

}
ข้อเขียนนี้ช่วยฉัน:  

No comments:

Post a Comment