我在我的应用中使用 Google+ 唱歌。当我点击 Google 按钮时, onConnect() 方法被调用,但我从 OnConnect() 方法中检索了一些来自 Google 的信息。但是“Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)”方法返回空值。请帮我

// Google API Services CallBack Method
        private boolean mSignInClicked;
        private ConnectionResult mConnectionResult;

        private void loginWithGoogle() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).addApi(Plus.API, null)
                    .addScope(Plus.SCOPE_PLUS_PROFILE)
                    .addScope(Plus.SCOPE_PLUS_LOGIN).build();

            mGoogleApiClient.connect();
        }

        @Override
        public void onConnected(Bundle arg0) {
            mSignInClicked = false;
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);

                String personName = currentPerson.getDisplayName();
                Log.i("personName", personName);

                Image personPhoto = currentPerson.getImage();
                String photoUrl = personPhoto.getUrl();
                Log.i("photoUrl", photoUrl);

                String personGooglePlusProfile = currentPerson.getUrl();
                Log.i("personGooglePlusProfile", personGooglePlusProfile);

                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                Log.i("email", email);

                boolean hasBirthday = currentPerson.hasBirthday();
                Log.i("hasBirthday", "" + hasBirthday);

                String birthday = currentPerson.getBirthday();
                Log.i("birthday", "" + birthday);

                boolean hasAbout = currentPerson.hasAboutMe();
                Log.i("hasAbout", "" + hasAbout);

                String about = currentPerson.getAboutMe();
                Log.i("about", "" + about);

                String Id = currentPerson.getId();
                Log.i("Id", Id);

                int gender = currentPerson.getGender();
                Log.i("gender", "" + gender);

                boolean hasLocation = currentPerson.hasCurrentLocation();
                Log.i("hasLocation", "" + hasLocation);

                String location = currentPerson.getCurrentLocation();
                Log.i("location", "" + location);

                AgeRange agerange = currentPerson.getAgeRange();
                if (agerange != null)
                    Log.i("agerange", "" + agerange.toString());

                Name name = currentPerson.getName();
                Log.i("getFamilyName", name.getFamilyName());

                btnGoogle = true;

                new ApiCall().execute(url);

                if (mGoogleApiClient.isConnected()) {
                    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                    mGoogleApiClient.disconnect();
                }
            } else {
                Log.i("personName", "getCurrentPerson(mGoogleApiClient) is null");
            }
        }

        @Override
        public void onConnectionSuspended(int arg0) {
            mGoogleApiClient.connect();
        }

        @Override
        public void onConnectionFailed(ConnectionResult result) {
            if (!mIntentInProgress) {
                mConnectionResult = result;
                if (mSignInClicked) {
                    // The user has already clicked 'sign-in' so we attempt to
                    // resolve
                    // all
                    // errors until the user is signed in, or they cancel.
                    resolveSignInError();
                }
            }
        }

        public void resolveSignInError() {
            if (mConnectionResult.hasResolution()) {
                try {
                    mIntentInProgress = true;
                    mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
                } catch (SendIntentException e) {
                    // The intent was canceled before it was sent. Return to the
                    // default
                    // state and attempt to connect to get an updated
                    // ConnectionResult.
                    mIntentInProgress = false;
                    mGoogleApiClient.connect();
                }
            }
        }

最佳答案

你必须添加这一行: Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);在 onConnected

像这样:

@Override
public void onConnected(Bundle connectionHint) {

    /* This Line is the key */
    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

    // After that  fetch data
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);

            String personName = currentPerson.getDisplayName();
            Log.i("personName", personName);
            ......
    }
}

引用:Retrieve the list of visible people in the user's circles

希望能帮到你ツ

关于android - 无法从Android中的Google+获取登录用户的信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26943474/

10-12 01:47