2016-06-13 48 views
0

我註冊用戶與谷歌在我的應用程序,然後我檢索那裏photoUrl,電子郵件和顯示名稱。profile.getEmail()檢索兩個字符串 - 一個電子郵件和另一個空白

問題是profile.getEmail();正在返回/檢索兩個字符串。一個是電子郵件,另一個是空白的。

這裏是我如何註冊他們:

public class SignupActivity extends AppCompatActivity { 

    private static final int RC_SIGN_IN = 1; 
    GoogleApiClient mGoogleApiClient; 
    FirebaseAuth mAuth; 
    TextView appName; 
    ProgressDialog signinProgressDialog; 
    CoordinatorLayout coordinatorLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_signup); 
//  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
//  setSupportActionBar(toolbar); 

     signinProgressDialog = new ProgressDialog(SignupActivity.this); 

     coordinatorLayout = (CoordinatorLayout) findViewById(R.id.signupCoordinatorLayout); 

     // Configure Google Sign In 
     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestIdToken(getString(R.string.o_auth_client_id)) 
       .requestEmail() 
       .requestProfile() 
       .requestId() 
       .build(); 

     // Build a GoogleApiClient with access to the Google Sign-In API and the 
     // options specified by gso. 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

        } 
       } /* OnConnectionFailedListener */) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 

     mAuth = FirebaseAuth.getInstance(); 

     findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (isNetworkAvailable()) { 
        signinProgressDialog.setMessage("Signing in..."); 
        signinProgressDialog.show(); 
        signIn(); 
       } else { 
        Snackbar snackbar = Snackbar 
          .make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG); 
        snackbar.show(); 
        signinProgressDialog.dismiss(); 
       } 
      } 
     }); 

    } 

    private void signIn() { 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
     if (requestCode == RC_SIGN_IN) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      if (result.isSuccess()) { 
       // Google Sign In was successful, authenticate with Firebase 
       GoogleSignInAccount account = result.getSignInAccount(); 
       firebaseAuthWithGoogle(account); 
      } else { 
       // Google Sign In failed, update UI appropriately 
       // ... 
      } 
     } 
    } 

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) { 
     Log.d("firebaseAuthWithGoogle", "firebaseAuthWithGoogle:" + acct.getId()); 

     AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d("signin_successful", "signInWithCredential:onComplete:" + task.isSuccessful()); 

         Intent mainActivityIntent = new Intent(SignupActivity.this, MainActivity.class); 
         mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
         startActivity(mainActivityIntent); 
         signinProgressDialog.dismiss(); 

         // If sign in fails, display a message to the user. If sign in succeeds 
         // the auth state listener will be notified and logic to handle the 
         // signed in user can be handled in the listener. 
         if (!task.isSuccessful()) { 
          Log.w("signin_unsuccessful", "signInWithCredential", task.getException()); 
          Toast.makeText(SignupActivity.this, "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 
         } 
         // ... 
        } 
       }); 
    } 

    @Override 
    public void onBackPressed() { 
     finish(); 
     super.onBackPressed(); 
    } 

    private boolean isNetworkAvailable() { 
     ConnectivityManager connectivityManager 
       = (ConnectivityManager) SignupActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
    } 
} 

這裏是我如何檢索要領:

public void retrieveUserInfo() { 
     FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
     if (user != null) { 
      for (UserInfo profile : user.getProviderData()) { 

       // Id of the provider (ex: google.com) 
       providerId = profile.getProviderId(); 

       // UID specific to the provider 
       uid = profile.getUid(); 

       // Name, email address, and profile photo Url 
       name = profile.getDisplayName(); 
       email = profile.getEmail(); 
       photoUrl = profile.getPhotoUrl().toString(); 

      } 
     } 
    } 

在這裏,當我把電子郵件字符串中Toast,我2敬酒,第一個顯示電子郵件,第二個空白。

我真的不知道這裏有什麼問題。

請讓我知道。

對不起,如果問題似乎格式不正確。我仍然是這裏的初學者。

+0

你的意思是火力返回一個字符串數組?你如何申報電子郵件?你嘗試過調試嗎? – adolfosrs

回答

1

更改retrieveUserInfo()中的代碼完成了這項工作。

這裏是改變的代碼:

public void retrieveUserInfo() { 
     FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
     if (user != null) { 
      // Id of the provider (ex: google.com) 
      providerId = user.getProviderId(); 

      // UID specific to the provider 
      uid = user.getUid(); 

      // Name, email address, and profile photo Url 
      name = user.getDisplayName(); 
      email = user.getEmail(); 
      photoUrl = user.getPhotoUrl().toString(); 

     } 
    } 
1

您正在使用for()循環也許這是造成問題,一旦它得到的字符串和其他時間沒有。

+0

去除for循環爲我做了工作。 –

相關問題