2016-01-06 56 views
6

在我的應用程序中,我正在使用Google Plus集成。還訪問Google帳戶的詳細信息包括用戶名,個人資料圖片等,但這些用戶的細節有時會返回空值。請幫我找出原因。Android 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.Please幫我找到了原因。

+0

沒有提供任何代碼,很難找到代碼中問題來自哪裏。請在提問前閱讀這些文檔 - http://stackoverflow.com/help/how-to-ask - 。 – jeffdill2

+0

@ jeffdill2對不起。問題編輯 – krishna

+0

@krishna使用此示例作爲參考可能對您有用.. https://github.com/googlesamples/google-services/blob/master/android/signin/app/src/main /java/com/google/samples/quickstart/signin/SignInActivity.java#L51-L55 –

回答

1

添加此行。

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

欲瞭解更多信息。閱讀Documentation

+0

這也返回空值 – krishna

+0

@krishna您的應用程序和谷歌控制檯之間的SHA1相同嗎? –

+0

是的,兩者都是一樣的。 – krishna