anuruddhika
anuruddhika

Reputation: 1559

How to get Facebook user name in android app

I added Facebook login function to my application. it was successfully connect and logout from the application.

I want to print that login user name in my application.

Can anyone help meto find this?

I used This as example

This is my code.

fbLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(fb.isSessionValid()){
                    try {
                        fb.logout(getApplicationContext());

                        updateButtonImage();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }else{

                    fb.authorize(MainActivity.this, new String[]{"email"}, new DialogListener() {

                        @Override
                        public void onFacebookError(FacebookError e) {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "fbError", Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onError(DialogError e) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onComplete(Bundle values) {
                            // TODO Auto-generated method stub
                            updateButtonImage();

                        }

                        @Override
                        public void onCancel() {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "onCancel", Toast.LENGTH_LONG).show();
                        }
                    });

                }

            }
        });

Upvotes: 0

Views: 1985

Answers (2)

Random
Random

Reputation: 429

I would follow this Facebook doc on how to fetch the user's data after he logged into your application.

Upvotes: 0

Bhavesh Vadalia
Bhavesh Vadalia

Reputation: 814

you need to refer this link facebook sdk and get information from user object.

Bundle params = new Bundle();
        params.putString("fields", "name,first_name,last_name");
        friendsRequest.setParameters(params);

        // Get current logged in user information
        Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {

            @Override
            public void onCompleted(GraphUser user, Response response) {
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Log.e(FriendSmashApplication.TAG, error.toString());
                    handleError(error, true);
                } else if (session == Session.getActiveSession()) {
                    // Set the currentFBUser attribute
                    ((FriendSmashApplication)getApplication()).setCurrentFBUser(user);
                }
            }
        });

or you can try this example.

http://www.androiddevelopersolution.com/2012/09/android-facebook-integration.html

Upvotes: 1

Related Questions