2011-02-26 72 views
5

我需要檢索用戶存儲在他的Gmail帳戶中的電子郵件地址。在我的應用程序中,用戶現在可以決定邀請他的一個朋友。我希望應用程序(如果用戶告訴我「好」)提出了用戶的聯繫人電子郵件地址列表存儲在Gmail中,其中他可以選擇一個或多個...如何獲取gmail用戶的聯繫人?

我知道存在Authentication and Authorization for Google APIs"。這是正確的方式嗎?而且,如何在Android中使用它們?

回答

-1

get contact info from android contact picker的問題和接受的答案有一個如何使用Android本地聯繫人選取器的示例。

+1

我不需要存儲在手機接觸的相關信息,而是保存在Gmail帳戶的電子郵件地址(即更那麼手機的) – Geltrude 2011-03-07 21:48:31

+2

由於Android手機通常在同步他們與Gmail聯繫人,聯繫人該手機應該與Gmail中的聯繫人相同。 – Roshan 2011-03-08 07:28:25

+1

但它取決於用戶,用戶是否想要同步他們的聯繫人。 – RobinHood 2015-03-24 11:42:47

3

我希望這會對像我這樣的人有所幫助,因爲我已經爲此搜尋了很多東西,並最終完成了以下任務。

我已使用GData java客戶端庫爲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; 

public class MainActivity extends Activity { 
    private URL feedUrl; 
    private static final String username="yourUsername"; 
    private static final String pwd="yourPassword"; 
    private ContactsService service; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     String url = "https://www.google.com/m8/feeds/contacts/default/full"; 

     try { 
      this.feedUrl = new URL(url); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 

     new GetTask().execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    private class GetTask extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 
      service = new ContactsService("ContactsSample"); 
      try { 
       service.setUserCredentials(username, pwd); 
      } catch (AuthenticationException e) { 
       e.printStackTrace(); 
      } 
      try { 
       queryEntries(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

    } 

    private void queryEntries() throws IOException, ServiceException{ 
     Query myQuery = new Query(feedUrl); 
     myQuery.setMaxResults(50); 
     myQuery.setStartIndex(1); 
     myQuery.setStringCustomParameter("showdeleted", "false"); 
     myQuery.setStringCustomParameter("requirealldeleted", "false"); 
//  myQuery.setStringCustomParameter("sortorder", "ascending"); 
//  myQuery.setStringCustomParameter("orderby", ""); 


     try{ 
      ContactFeed resultFeed = (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"); 
     } 

    } 
    private void printContact(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); 

     Link photoLink = contact.getLink("http://schemas.google.com/contacts/2008/rel#photo", "image/*"); 
     if (photoLink.getEtag() != null) { 
      Service.GDataRequest request = service.createLinkQueryRequest(photoLink); 

      request.execute(); 
      InputStream in = request.getResponseStream(); 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      RandomAccessFile file = new RandomAccessFile("/tmp/" + contact.getSelfLink().getHref().substring(contact.getSelfLink().getHref().lastIndexOf('/') + 1), "rw"); 

      byte[] buffer = new byte[4096]; 
      for (int read = 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()); 
     String photoEtag = 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"); 
    } 

} 


所需的庫文件:您可以從here

  • GDATA,客戶1.0.jar
  • GDATA客戶端 - 元1.0.jar
  • GDATA的聯繫人獲得這些罐子-3.0.jar
  • gdata-contacts-meta-3.0.jar
  • gdata-core-1.0.jar
  • 番石榴11.0.2.jar

注意:添加Internet權限在AndroidManifest文件。

<uses-permission android:name="android.permission.INTERNET"/> 
+0

這裏是番石榴的鏈接http://code.google.com/p/guava-libraries/ – 2014-06-18 05:30:23

+0

這不起作用 – 2017-04-27 13:49:33