How To Get Gmail User's Contacts?
I need to retrieve the email addresses that the user has stored in his gmail account. In my app, the user can now decide to invite a friend of him. I want that the application (if
Solution 1:
I hope this will help for someone like me, because I have searched a lot for this and finally done with the below. I have used GData java client library for Google Contacts API v3.
package com.example.cand;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import com.google.gdata.client.Query;
import com.google.gdata.client.Service;
import com.google.gdata.client.contacts.ContactsService;
import com.google.gdata.data.Link;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.NoLongerAvailableException;
import com.google.gdata.util.ServiceException;
publicclassMainActivityextendsActivity {
private URL feedUrl;
privatestaticfinal String username="yourUsername";
privatestaticfinal String pwd="yourPassword";
private ContactsService service;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stringurl="https://www.google.com/m8/feeds/contacts/default/full";
try {
this.feedUrl = newURL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
newGetTask().execute();
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
privateclassGetTaskextendsAsyncTask<Void, Void, Void>{
@Overrideprotected Void doInBackground(Void... params) {
service = newContactsService("ContactsSample");
try {
service.setUserCredentials(username, pwd);
} catch (AuthenticationException e) {
e.printStackTrace();
}
try {
queryEntries();
} catch (Exception e) {
e.printStackTrace();
}
returnnull;
}
}
privatevoidqueryEntries()throws IOException, ServiceException{
QuerymyQuery=newQuery(feedUrl);
myQuery.setMaxResults(50);
myQuery.setStartIndex(1);
myQuery.setStringCustomParameter("showdeleted", "false");
myQuery.setStringCustomParameter("requirealldeleted", "false");
// myQuery.setStringCustomParameter("sortorder", "ascending");// myQuery.setStringCustomParameter("orderby", "");try{
ContactFeedresultFeed= (ContactFeed)this.service.query(myQuery, ContactFeed.class);
for (ContactEntry entry : resultFeed.getEntries()) {
printContact(entry);
}
System.err.println("Total: " + resultFeed.getEntries().size() + " entries found");
}
catch (NoLongerAvailableException ex) {
System.err.println("Not all placehorders of deleted entries are available");
}
}
privatevoidprintContact(ContactEntry contact)throws IOException, ServiceException{
System.err.println("Id: " + contact.getId());
if (contact.getTitle() != null)
System.err.println("Contact name: " + contact.getTitle().getPlainText());
else {
System.err.println("Contact has no name");
}
System.err.println("Last updated: " + contact.getUpdated().toUiString());
if (contact.hasDeleted()) {
System.err.println("Deleted:");
}
// ElementHelper.printContact(System.err, contact);LinkphotoLink= contact.getLink("http://schemas.google.com/contacts/2008/rel#photo", "image/*");
if (photoLink.getEtag() != null) {
Service.GDataRequestrequest= service.createLinkQueryRequest(photoLink);
request.execute();
InputStreamin= request.getResponseStream();
ByteArrayOutputStreamout=newByteArrayOutputStream();
RandomAccessFilefile=newRandomAccessFile("/tmp/" + contact.getSelfLink().getHref().substring(contact.getSelfLink().getHref().lastIndexOf('/') + 1), "rw");
byte[] buffer = newbyte[4096];
for (intread=0; (read = in.read(buffer)) != -1; )
out.write(buffer, 0, read);
file.write(out.toByteArray());
file.close();
in.close();
request.end();
}
System.err.println("Photo link: " + photoLink.getHref());
StringphotoEtag= photoLink.getEtag();
System.err.println(" Photo ETag: " + (photoEtag != null ? photoEtag : "(No contact photo uploaded)"));
System.err.println("Self link: " + contact.getSelfLink().getHref());
System.err.println("Edit link: " + contact.getEditLink().getHref());
System.err.println("ETag: " + contact.getEtag());
System.err.println("-------------------------------------------\n");
}
}
Required library files: you can get these jars from here
- gdata-client-1.0.jar
- gdata-client-meta-1.0.jar
- gdata-contacts-3.0.jar
- gdata-contacts-meta-3.0.jar
- gdata-core-1.0.jar
- guava-11.0.2.jar
Note: Add internet permission in AndroidManifest file.
<uses-permissionandroid:name="android.permission.INTERNET"/>
Post a Comment for "How To Get Gmail User's Contacts?"