krishna
krishna

Reputation: 409

Android Google + integration sometimes return null values

In my application i am using Google Plus integration. Also access Google account details include username, profile image etc. But these user details some times return null value. Please help me to find the reason.

This is my code:

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

and in onConnected():

   @Override
    public void onConnected(Bundle bundle) {

            String personName="Unknown";

                gmail = Plus.AccountApi.getAccountName(
                        (GoogleApiClient) mGoogleApiClient).toString();
                try {

                    String[] id = gmail.split("@");
                    try {
                        plusid = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getId();
                    } catch (NullPointerException e) {
                        plusid = id[0];
                    }

                    plusimage = Plus.PeopleApi
                            .getCurrentPerson((GoogleApiClient) mGoogleApiClient)
                            .getImage().getUrl().toString();

                    plusname = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient).getDisplayName().toString();

                }
                if (CheckNetworkConnection.isConnectionAvailable(MainActivity.this)) {
                 //   new SocialLogin().execute();
                }
                } catch (NullPointerException e) {
                    Toast.makeText(getApplicationContext(), "GMAIL" + gmail, Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "ID" +plusid , Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "NAME" + plusname, Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "IMG" + plusimage, Toast.LENGTH_SHORT).show();
                    Toast.makeText(MainActivity.this, "Google plus account not configured correctly", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }

here plusimage and plusname return null.Please help me to find the reason.

Upvotes: 6

Views: 644

Answers (1)

Mansukh Ahir
Mansukh Ahir

Reputation: 3553

Add this line.

 Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

like.

public void onConnected(Bundle connectionHint) {    

    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

    String personName="Unknown";
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
       Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
       String personName = currentPerson.getDisplayName();
       String personPhoto = currentPerson.getImage();
       String personGooglePlusProfile = currentPerson.getUrl();
    }
}

for more info. read Documentation

Upvotes: 1

Related Questions