在Android

时间:2016-09-05 06:31:45

标签: android android-contacts gdata google-content-api

我正在尝试使用以下代码在Android应用程序中使用Google通讯录API访问我的Google帐户联系人。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        ContactsService myService = new ContactsService("GoogleContactsProject");


        GoogleOAuthParameters oauthParam = new GoogleOAuthParameters();
        oauthParam.setOAuthConsumerKey("9****************u9473opkpetc*****v3.apps.googleusercontent.com");
        oauthParam.setOAuthConsumerSecret("A*****************415yy6vAipya4");
        oauthParam.setOAuthType(OAuthParameters.OAuthType.TWO_LEGGED_OAUTH);

        myService.setOAuthCredentials(oauthParam, new OAuthHmacSha1Signer());

        (new HelloThread(myService)).start();


    } catch (Exception e) {
        e.printStackTrace();
    }

}

public class HelloThread extends Thread {
    ContactsService myServ;

    HelloThread(ContactsService myService) {
        this.myServ = myService;
    }

    public void run() {
        System.out.println("Hello from a thread!");

        try {
            printAllContacts(myServ);


        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

public static void printAllContacts(ContactsService myService)
        throws ServiceException, IOException {


    // Request the feed
    URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
    ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
    // Print the results
    System.out.println(resultFeed.getTitle().getPlainText());
    for (ContactEntry entry : resultFeed.getEntries()) {
        if (entry.hasName()) {
            Name name = entry.getName();
            if (name.hasFullName()) {
                String fullNameToDisplay = name.getFullName().getValue();
                if (name.getFullName().hasYomi()) {
                    fullNameToDisplay += " (" + name.getFullName().getYomi() + ")";
                }
                System.out.println("\t\t" + fullNameToDisplay);
            } else {
                System.out.println("\t\t (no full name found)");
            }
            if (name.hasNamePrefix()) {
                System.out.println("\t\t" + name.getNamePrefix().getValue());
            } else {
                System.out.println("\t\t (no name prefix found)");
            }
            if (name.hasGivenName()) {
                String givenNameToDisplay = name.getGivenName().getValue();
                if (name.getGivenName().hasYomi()) {
                    givenNameToDisplay += " (" + name.getGivenName().getYomi() + ")";
                }
                System.out.println("\t\t" + givenNameToDisplay);
            } else {
                System.out.println("\t\t (no given name found)");
            }
            if (name.hasAdditionalName()) {
                String additionalNameToDisplay = name.getAdditionalName().getValue();
                if (name.getAdditionalName().hasYomi()) {
                    additionalNameToDisplay += " (" + name.getAdditionalName().getYomi() + ")";
                }
                System.out.println("\t\t" + additionalNameToDisplay);
            } else {
                System.out.println("\t\t (no additional name found)");
            }
            if (name.hasFamilyName()) {
                String familyNameToDisplay = name.getFamilyName().getValue();
                if (name.getFamilyName().hasYomi()) {
                    familyNameToDisplay += " (" + name.getFamilyName().getYomi() + ")";
                }
                System.out.println("\t\t" + familyNameToDisplay);
            } else {
                System.out.println("\t\t (no family name found)");
            }
            if (name.hasNameSuffix()) {
                System.out.println("\t\t" + name.getNameSuffix().getValue());
            } else {
                System.out.println("\t\t (no name suffix found)");
            }
        } else {
            System.out.println("\t (no name found)");
        }
        System.out.println("Email addresses:");
        for (Email email : entry.getEmailAddresses()) {
            System.out.print(" " + email.getAddress());
            if (email.getRel() != null) {
                System.out.print(" rel:" + email.getRel());
            }
            if (email.getLabel() != null) {
                System.out.print(" label:" + email.getLabel());
            }
            if (email.getPrimary()) {
                System.out.print(" (primary) ");
            }
            System.out.print("\n");
        }
        System.out.println("IM addresses:");
        for (Im im : entry.getImAddresses()) {
            System.out.print(" " + im.getAddress());
            if (im.getLabel() != null) {
                System.out.print(" label:" + im.getLabel());
            }
            if (im.getRel() != null) {
                System.out.print(" rel:" + im.getRel());
            }
            if (im.getProtocol() != null) {
                System.out.print(" protocol:" + im.getProtocol());
            }
            if (im.getPrimary()) {
                System.out.print(" (primary) ");
            }
            System.out.print("\n");
        }
        System.out.println("Groups:");
        for (GroupMembershipInfo group : entry.getGroupMembershipInfos()) {
            String groupHref = group.getHref();
            System.out.println("  Id: " + groupHref);
        }
        System.out.println("Extended Properties:");
        for (ExtendedProperty property : entry.getExtendedProperties()) {
            if (property.getValue() != null) {
                System.out.println("  " + property.getName() + "(value) = " +
                        property.getValue());
            } else if (property.getXmlBlob() != null) {
                System.out.println("  " + property.getName() + "(xmlBlob)= " +
                        property.getXmlBlob().getBlob());
            }
        }
        Link photoLink = entry.getContactPhotoLink();
        String photoLinkHref = photoLink.getHref();
        System.out.println("Photo Link: " + photoLinkHref);
        if (photoLink.getEtag() != null) {
            System.out.println("Contact Photo's ETag: " + photoLink.getEtag());
        }
        System.out.println("Contact's ETag: " + entry.getEtag());
    }
}

但是我没有收到像

这样的错误

进程:com.itconquest.myapplication,PID:3697                                                                             java.lang.NullPointerException:没有身份验证标头信息                                                                                 在com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)                                                                                 在com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)                                                                                 在com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)                                                                                 在com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)                                                                                 在com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)                                                                                 在com.google.gdata.client.Service.getFeed(Service.java:1135)                                                                                 在com.google.gdata.client.Service.getFeed(Service.java:998)                                                                                 在com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)                                                                                 在com.google.gdata.client.Service.getFeed(Service.java:1017)                                                                                 at com.itconquest.myapplication.MainActivity.printAllContacts(MainActivity.java:82)                                                                                 at com.itconquest.myapplication.MainActivity $ HelloThread.run(MainActivity.java:60) 09-05 12:28:45.194 3697-3772 / com.itconquest.myapplication E / Surface:getSlotFromBufferLocked:unknown buffer:0xae3f3b50

这是我的Gradle文件

apply plugin: 'com.android.application'

android {     compileSdkVersion 24     buildToolsVersion“24.0.2”

defaultConfig {
    applicationId "com.itconquest.myapplication"
    minSdkVersion 15
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

} 存储库{     mavenCentral() } buildscript {     存储库{         maven {url'http://repo1.maven.org/maven2'}     }

} 依赖{     编译fileTree(dir:'libs',包括:['* .jar'])     testCompile'junit:junit:4.12'     编译'com.android.support:appcompat-v7:24.2.0'     编译'com.google.api-client:google-api-client:1.22.0'     编译'com.google.gdata:core:1.47.1' }

我的谷歌帐户scree拍摄如下:

注意:我已将OAuth 2.0客户端ID用作setOAuthConsumerKey和 API密钥为setOAuthConsumerSecret

enter image description here

如何解决此问题?

0 个答案:

没有答案