Android Google People API 连接始终为空

标签 android google-api-client google-people-api

我正在尝试运行 Google People API sample code按照步骤 here 。不幸的是,它总是从 ListConnectionsResponse 的 response.getConnections() 获得空连接。登录成功后,在 onActivityResult 中执行以下 AsyncTask:

class PeoplesAsync extends AsyncTask<String, Void, List<String>> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        updateUI();
    }

    @Override
    protected List<String> doInBackground(String... params) {

        List<String> nameList = new ArrayList<>();

        try {
            People peopleService = PeopleHelper.setUp(MainActivity.this, params[0]);

            ListConnectionsResponse response = peopleService.people().connections()
                    .list("people/me").setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                    .execute();
            List<Person> connections = response.getConnections();

            //error: connections is always null
            for (Person person : connections) {
                if (!person.isEmpty()) {
                    List<Name> names = person.getNames();
                    List<EmailAddress> emailAddresses = person.getEmailAddresses();
                    List<PhoneNumber> phoneNumbers = person.getPhoneNumbers();

                    if (phoneNumbers != null)
                        for (PhoneNumber phoneNumber : phoneNumbers)
                            Log.d(TAG, "phone: " + phoneNumber.getValue());

                    if (emailAddresses != null)
                        for (EmailAddress emailAddress : emailAddresses)
                            Log.d(TAG, "email: " + emailAddress.getValue());

                    if (names != null)
                        for (Name name : names)
                            nameList.add(name.getDisplayName());

                }
            }

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

        return nameList;
    }


    @Override
    protected void onPostExecute(List<String> nameList) {
        super.onPostExecute(nameList);

        progressBar.setVisibility(View.GONE);

        recyclerView.setVisibility(View.VISIBLE);

        adapter = new PeopleAdapter(nameList);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView.setAdapter(adapter);

    }
}

导致 Google People API 中的空响应连接的原因是什么以及如何修复?

最佳答案

真是一个误解,ListConnectionsResponse response = peopleService.people().connections() .list("people/me") 不适用于用户的个人资料,它适用于用户连接的个人资料。如果您需要用户的个人资料信息:

Person profile = peopleService.people().get("people/me").execute();

            if (!profile.isEmpty()) {

                List<Name> names = profile.getNames();
                List<Birthday> birthdays = profile.getBirthdays();
                List<Gender> genders = profile.getGenders();
                List<Url> urls = profile.getUrls();
                List<EmailAddress> emailAddresses = profile.getEmailAddresses();
                List<Photo> profileImages = profile.getPhotos();

                String displayName = names.get(0).getDisplayName();
                String birthday = birthdays.get(0).getText();
                String gender = genders.get(0).getValue();
                String email = emailAddresses.get(0).getValue();
                String profileImage = profileImages.get(0).getUrl();

            }

关于Android Google People API 连接始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039563/

相关文章:

java - 由 : java. lang.IllegalStateException 引起:GoogleApiClient 尚未连接

google-api - 如何在来自特定用户的 Google People API 响应中唯一标识个人资源?

安卓并发问题

android - 在不破坏现有小部件的情况下重命名 AppWidgetProvider

java - Android:无法从具有 1 行、1 列的 CursorWindow 读取第 0 行、第 -1 列

ruby - 上传到谷歌云存储中的嵌套桶

android - 在 Parse.com 中使用 ParseUser 更改密码和忘记密码

android - GoogleApiClient 似乎没有对 disconnect() API 的回调...?

google-people-api - Google People API 空引荐来源网址(但引荐来源网址是在控制台中设置的)

android - 如何使用 People API 获取有关 "other contacts"的完整信息?