获得Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)null值

时间:2016-04-19 10:49:47

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

这是onConnected方法:

try
    {   mGoogleApiClient.connect();
        Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) 
        {
           Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            googlePlusProfilrInfo(currentPerson);
            isGooglePlusClicked = false;
        } else {
            Toast.makeText(getApplicationContext(),
                    "No Personal info mention", Toast.LENGTH_LONG).show();

        }

    } 

oncreate中的BuildGooglePlus API

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API,Plus.PlusOptions.builder().build())                   
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();

onClick

public void onClick(View v) {
    if(Utils.isConnectedToInternet(LoginActivity.this)){
        switch (v.getId()) {
        case R.id.img_login_google_plus:

             signInWithGplus();
            isGooglePlusClicked = true;
            Log.i(TAG, "onClick isGooglePlusClicked value=" + isGooglePlusClicked);

            if (!mGoogleApiClient.isConnecting()) {
                resolveSignInError();
            } else {
                Toast.makeText(
                        this,
                        "Google sign-in not available for now. Please try again later.",
                        Toast.LENGTH_SHORT).show();
                Log.i(TAG, "else case");
            }

            break;


private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
       isGooglePlusClicked = true;
        resolveSignInError();
    }

}


public void SignOutFromGPlus()
{
    if(mGoogleApiClient.isConnected())
    {
        Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        mGoogleApiClient.connect();

    }
}

@Override
public void onConnectionSuspended(int cause) {
    // The connection to Google Play services was lost for some reason.
    // We call connect() to attempt to re-establish the connection or get a
    // ConnectionResult that we can attempt to resolve.
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {
      if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
                    0).show();
            return;
        }

        if (!mIntentInProgress) {
            // Store the ConnectionResult for later usage
            mConnectionResult = result;

            if (gServicesSignInProg == STATE_SIGN_IN) {
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError();
            }
        }
}

private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }

}

@Override
public void onConnected(Bundle connectionHint) {        

    // Reaching onConnected means we consider the user signed in.
    // Update the user interface to reflect that the user is signed in.
    // Retrieve some profile information to personalize our app for the
    // user.
    try
    {   mGoogleApiClient.connect();
        Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) 
        {
           Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            googlePlusProfilrInfo(currentPerson);
            isGooglePlusClicked = false;
        } else {
            Toast.makeText(getApplicationContext(),
                    "No Personal info mention", Toast.LENGTH_LONG).show();

        }

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

}

持续获得无效人值... 请解决这个问题......

0 个答案:

没有答案