android - 使用 Google +/PlusClient 获取个人信息

标签 android google-plus

我正在尝试获取 Google+ 用户的基本信息以登录我的应用。

我可以连接并获取帐户名:

String email = mPlusClient.getAccountName(); // This works, gives me the email

然后我尝试通过以下方式获取更多信息:

        mPlusClient.loadVisiblePeople(new OnPeopleLoadedListener() {

        @Override
        public void onPeopleLoaded(ConnectionResult status,
                PersonBuffer personBuffer, String nextPageToken) {

            Log.i("", "persons loaded result = " + status.toString()
                    + ", personsCount = " + personBuffer.getCount()
                    + ", token = " + nextPageToken);
            if (status.isSuccess()) {
                Iterator<Person> itP = personBuffer.iterator();
                while (itP.hasNext()) {
                    Person person = itP.next();
                    Toast.makeText(getApplicationContext(), person.getNickname(), 5).show();
                }
            }
        }
    }, null);

我可以获取好友信息

persons loaded result = ConnectionResult{statusCode=SUCCESS, resolution=null}, personsCount = 15, token = null

但是如果我找不到如何获取个人信息...

当我尝试时:

    Person p = mPlusClient.getCurrentPerson();

p 为空,

当我尝试时:

mPlusClient.loadPerson(this, "me");

无法识别 loadPerson

所以我不知道该怎么做......

如有任何帮助,我们将不胜感激!

最佳答案

你可以试试下面的代码来获取当前用户的个人资料信息:

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(Plus.API)
                            .addScope(Plus.SCOPE_PLUS_LOGIN)
                            .build();

if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
                if(Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null){
                    Person mCurrentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                    String displayName = mCurrentPerson.getDisplayName();
                    int mUserIdOfCurrentUser = mCurrentPerson.getId();
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE, "DisplayName: "+displayName);
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Id: "+ mCurrentPerson.getId());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Image: "+mCurrentPerson.getImage().getUrl());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"List of Urls: "+mCurrentPerson.getUrls());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Current Url: "+mCurrentPerson.getUrl());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Cover: "+mCurrentPerson.getCover());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Object type: "+mCurrentPerson.getObjectType());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"User Id : "+mUserIdOfCurrentUser);
                }

关于android - 使用 Google +/PlusClient 获取个人信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903088/

相关文章:

android - 如何以编程方式添加到布局复合组件 Android?

java - CGLIB 可以在 Android 上使用吗?

google-plus - Google+ API 是否允许您获取圈子计数?

android - Android 平台上的 Google+ API 和 Google+ 登录弃用和停用

android - 有没有办法从命令行从传感器获取数据?

android - 模糊背景图像,如 android 中的雅虎天气应用程序

android - 这是 Android GregorianCalendar 中的错误吗?

api - 用于读取 "plus one"计数的 Google+ API

python - 按集合列出 Google+ 事件

javascript - 是否有类似于 Google+ 反馈工具的服务?