【问题标题】:Android Google People API connection is always nullAndroid Google People API 连接始终为空
【发布时间】:2016-10-14 09:23:45
【问题描述】:

我正在尝试按照here 的步骤运行 Google People API sample code。不幸的是,它总是从 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 中导致空响应连接的原因是什么以及如何解决?

【问题讨论】:

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


    【解决方案1】:

    误会了,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();
    
                }
    

    【讨论】:

    • 但是为什么还是 response.getConnections();问题中为空?
    • 当你在列表中没有朋友时我相信
    • 我得到了空字符串。我正在使用您的代码。我正在获取名称、ImageUrl 和电子邮件 ID,但其他字段返回为空。你能帮我吗?
    • @JayDangar 您在您的帐户中输入了这些字段吗?
    • @BeeingJk,我已经提供了客户 ID 和机密 ID,但没有用。
    【解决方案2】:

    您是否创建了 API 控制台项目?我找到了here 一个类似 API 的教程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-20
      • 2018-03-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多