Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zybnet.contact.fix;
- import com.google.gdata.client.contacts.ContactQuery;
- import com.google.gdata.client.contacts.ContactsService;
- import com.google.gdata.data.contacts.ContactEntry;
- import com.google.gdata.data.contacts.ContactFeed;
- import com.google.gdata.data.extensions.FullName;
- import com.google.gdata.data.extensions.PhoneNumber;
- import com.google.gdata.util.AuthenticationException;
- import com.google.gdata.util.ServiceException;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.List;
- /**
- *
- * @author Raffaele
- */
- public class Main {
- /**
- * @param args username and password
- */
- public static void main(String[] args) throws AuthenticationException, MalformedURLException, IOException, ServiceException {
- ContactsService service = new ContactsService("zybnet-fix-android");
- service.setUserCredentials(args[0], args[1]);
- URL getAllURL = new URL("https://www.google.com/m8/feeds/contacts/" + args[0] + "/full");
- ContactQuery query = new ContactQuery(getAllURL);
- query.setMaxResults(10000);
- ContactFeed feed = service.getFeed(query, ContactFeed.class);
- int counter = 0;
- for (ContactEntry entry: feed.getEntries()) {
- if (entry.hasName() && entry.hasPhoneNumbers()) {
- counter++;
- printNumbers(entry);
- List<PhoneNumber> numbers = entry.getPhoneNumbers();
- for (PhoneNumber number : numbers) {
- setRel(entry, number);
- }
- }
- }
- System.out.println(String.format("Founded %d elements", counter));
- }
- private static void setRel(ContactEntry entry, PhoneNumber number) {
- if ("http://schemas.google.com/g/2005#mobile".equals(number.getRel()))
- return;
- try {
- number.setRel("http://schemas.google.com/g/2005#mobile");
- ContactEntry updated = entry.update();
- System.out.println("Update rel: OK");
- } catch(com.google.gdata.util.InvalidEntryException e) {
- number.setLabel(null);
- try {
- entry.update();
- } catch (Exception e2) {
- System.err.println("Another Failure. Giving up on " + entry.getName().getFullName().getValue());
- }
- }catch (Exception e) {
- System.err.println("Error in updating rel: " + e.getClass().getCanonicalName());
- }
- }
- private static void printNumbers(ContactEntry entry) {
- FullName name = entry.getName().getFullName();
- for (PhoneNumber number : entry.getPhoneNumbers())
- System.out.println(String.format("%s: %s [%s]", name.getValue(), number.getPhoneNumber(), number.getRel()));
- }
- }
Add Comment
Please, Sign In to add comment