Hammad Nasir Hammad Nasir -5 years ago 140
Android Question

profile.getEmail() retrieving two string - one email and another blank

I'm signing up users with google in my app and then I'm retrieving there photoUrl, email, and display name.

The problem is that the

is returning/retrieving two strings. One is email and another is blank.

Here's how I'm signing up them:

public class SignupActivity extends AppCompatActivity {

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

protected void onCreate(Bundle savedInstanceState) {
// 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)

// 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() {
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

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

mAuth = FirebaseAuth.getInstance();

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


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

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();
} 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);
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("signin_successful", "signInWithCredential:onComplete:" + task.isSuccessful());

Intent mainActivityIntent = new Intent(SignupActivity.this, MainActivity.class);

// 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.",
// ...

public void onBackPressed() {

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

Here's how I'm retrieving the essentials:

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


Here, when I placed the email string in a
, I got 2 toasts, first one displayed the email and second one was blank.

I really don't know what's wrong here.

Please let me know.

Sorry if question seems to be badly formatted. I'm still a beginner here.

Answer Source

you are using for() loop maybe that's causing problem, once it gets the string and other time do not.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download