Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: cannot display Facebook user profile information into android studio after authenticating

Fathy Eldesouky:

i have spent maybe 4 days and watched 100 videos about this issue , its my first question here on stackoverflow and also it is my first application ever i hope someone will help me this is my Login activity which i included the Facebook login for :


public class LoginActivity extends AppCompatActivity {
Button createaccount;
Button login;
private FirebaseAuth mAuth;
ProgressBar progressBar;
public static final String EXTRA_MESSAGE = "com.fathy.reufamnew.MESSAGE";
TextView testys;
private EditText mEmailView;
private EditText mPasswordView;
LoginButton facebutton;
ImageView imgavatar;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbackManager=CallbackManager.Factory.create();
setContentView(R.layout.activity_login);
login=(Button)findViewById(R.id.login);
mAuth=FirebaseAuth.getInstance();
facebutton=(LoginButton)findViewById(R.id.fb_login);
testys=(TextView)findViewById(R.id.testys) ;
progressBar=(ProgressBar)findViewById(R.id.progressbar);
mEmailView = (EditText) findViewById(R.id.emailnow);
mPasswordView = (EditText) findViewById(R.id.password1);

facebutton.setReadPermissions(Arrays.asList("public_profile","email","user_birthday","user_hometown","user_about_me"));
facebutton.registerCallback(callbackManager, new FacebookCallback () {
@Override
public void onSuccess(LoginResult loginResult) {

String accesstoken =loginResult.getAccessToken().getToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Intent intent=new Intent(LoginActivity.this,Facebook.class);
intent.putExtra("object",object.toString());
startActivity(intent);

Log.v("response", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, age_range,last_name, email, birthday, gender,location.fields(location)"); // Parámetros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
testys.setText("Login Sucess\n"+loginResult.getAccessToken().getUserId());
}

@Override
public void onCancel() {
testys.setText("login canceled");
}

@Override
public void onError(FacebookException error) {

}
});
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode,resultCode,data);
}


private void attemptLogin() {

login=(Button)findViewById(R.id.login);
// Store values at the time of the login attempt.
String email = mEmailView.getText().toString();
String password = mPasswordView.getText().toString();

if(email.isEmpty()) {
mEmailView.setError("E-mail is Required");
mEmailView.requestFocus();
return;
}

if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
mEmailView.setError("Please Enter A Valid Email");
mEmailView.requestFocus();
return;

}
if(password.isEmpty()) {
mPasswordView.setError("Password is Required");
mPasswordView.requestFocus();
return;

}
if(password.length() mPasswordView.setError("Password is too short(Not Less than 6)");
mPasswordView.requestFocus();
return;

}
Toast.makeText(this, "Login in Progress", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.VISIBLE);

mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener () {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
finish();
progressBar.setVisibility(View.GONE);
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
progressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),task.getException().getMessage(),Toast.LENGTH_LONG).show(); }
}

});
}


public void showerrordialog(String message){
new AlertDialog.Builder(this)
.setTitle("OoOps")
.setMessage(message)
.setPositiveButton(android.R.string.ok,null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
public void createaccountonclick(View view){
createaccount=(Button)findViewById(R.id.createaccount);
Intent intent=new Intent(this,UserProfile.class);
finish();
startActivity(intent);
}
public void LogMeIn(View view){
login=(Button)findViewById(R.id.login);
attemptLogin();
}

@Override
protected void onStart() {
super.onStart();
if(mAuth.getCurrentUser()!=null){
finish();
Intent intent=new Intent(LoginActivity.this,RestoreProfileData.class);
startActivity(intent);
}
}

}

and this the second activity i named it facebook which will get the profile picture , name email and gender


public class Facebook extends AppCompatActivity {
TextView email;
TextView gender;
TextView facebookName;
LinearLayout infoLayout;
ProfilePictureView profilePictureView;
ImageView ima;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook);
infoLayout = (LinearLayout) findViewById(R.id.layout_info);
profilePictureView = (ProfilePictureView) findViewById(R.id.image);
gender = (TextView) findViewById(R.id.gender);
ima=(ImageView)findViewById(R.id.imaging);
facebookName = (TextView) findViewById(R.id.facebookname);
email = (TextView) findViewById(R.id.email);
try {
JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("object"));
getdata(jsonObj);
} catch (JSONException e) {
e.printStackTrace();
}



}

private void getdata(JSONObject object) {

try{


URL profile_pic =new URL("http://ift.tt/wljqS4"+object.getString("id")+"/picture?width=250&height=250");

Picasso.with(this).load(profile_pic.toString()).into(ima);
String fathy= object.getString("email");
email.setText(fathy);
facebookName.setText(object.getString("first_name"));
gender.setText(object.getString("gender"));

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}

}

I want to get the name , gender and email of the user who logged in with facebook credentials

i am just getting the profile picture .of the user but the other data are empty with no error



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: cannot display Facebook user profile information into android studio after authenticating

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×