因需要初步研究了一下google contacts api
写了简单的工具类,大家可以转用,有问题可以一同探讨
public class backUp {
private String baseContactsUrl="https://www.google.com/m8/feeds/contacts/default/full";
private ContactsService myService;
public backUp(String username, String password) throws Exception {
myService=new ContactsService("shining-backup-1");
myService.setUserCredentials(username, password);
}
/**
* Create A new group Named by the given name
*
* @author shining
* @param service
* @param name
* @param notes
* @return ContactGroupEntry
* @throws Exception
*/
public ContactGroupEntry createContactGroupEntry(String name) throws Exception
{
ContactGroupEntry group = new ContactGroupEntry();
group.setTitle(new PlainTextConstruct(name));
URL postUrl = new URL("https://www.google.com/m8/feeds/groups/default/full");
return myService.insert(postUrl, group);
}
/**
* Get contacts from a group by groupId (Always <=25, defined by google)
*
* @author shining
* @param myService
* @param groupId
* @return List<ContactEntry>
* @throws ServiceException
* @throws IOException
*/
public List<ContactEntry> getAGroupContactResults(String groupId) throws ServiceException,
IOException {
URL feedUrl = new URL(baseContactsUrl);
ContactQuery myQuery = new ContactQuery(feedUrl);
myQuery.setStringCustomParameter("group", groupId);
ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);
return resultFeed.getEntries();
}
/**
* Create A contact into a group by groupId useing the following params
*
* @author shining
* @param fullName
* @param givenName
* @param familyName
* @param phone
* @param bir
* @param groupId
* @param email
* @param notes
* @return ContactEntry
* @throws ServiceException
* @throws IOException
*/
public ContactEntry createAContact(
String fullName, String givenName, String familyName, String phone,String bir,
String groupId,String email,String notes)
throws ServiceException, IOException {
ContactEntry contact = new ContactEntry();
Name name = new Name();
name.setFullName(new FullName(fullName,null));
name.setGivenName(new GivenName(givenName,null));
name.setFamilyName(new FamilyName(familyName,null));
contact.setName(name);
//set other Inf, Chinese Called Beizhu
contact.setContent(new PlainTextConstruct(notes));
//set email
Email primaryMail = new Email();
primaryMail.setAddress(email);
primaryMail.setRel("http://schemas.google.com/g/2005#home");
primaryMail.setPrimary(true);
contact.addEmailAddress(primaryMail);
PhoneNumber pn=new PhoneNumber();
pn.setPhoneNumber(phone);
pn.setPrimary(true);
pn.setRel("http://schemas.google.com/g/2005#work");
contact.addPhoneNumber(pn);
//Add to a Group
GroupMembershipInfo gm=new GroupMembershipInfo();
gm.setHref(groupId);
contact.addGroupMembershipInfo(gm);
Birthday b=new Birthday(bir);
contact.setBirthday(b);
//Add process
URL postUrl = new URL(baseContactsUrl);
return myService.insert(postUrl, contact);
}
}