将联系人备份至google contacts

因需要初步研究了一下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);

}

}


 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值