Android Google +集成有时会返回null值

时间:2016-01-06 15:52:49

标签: android google-plus

在我的应用程序中,我正在使用Google Plus集成。另外访问Google帐户详细信息包括用户名,个人资料图片等。但这些用户详细信息有时会返回空值。请帮我找出原因。

这是我的代码:

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();

并在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();
                }
            }

这里plusimage和plusname返回null。请帮我找到原因。

1 个答案:

答案 0 :(得分:1)

添加此行。

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

等。

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();
    }
}

了解更多信息。阅读Documentation