3

I have successfully installed the android sdk v4.0 in my studio. I get to login correctly using LoginButton.

So far everything ok.

I want to have a string for each user data collecting interests me, for example:

String name = USER_NAME String = USER_MAIL mail.

With the 3.0 sdk had it properly.

With the new SDK can not I do this, I followed the instructions in this thread:

Android Facebook 4.0 SDK How to get Email, Date of Birth and gender of User

I read in the documentation and in various threads about "Using the Graph API" but I can not get the data I'm looking for and assign them to each string.

this is my code (just as it is now):

LoginButton loginButton;
private CallbackManager mCallbackManager;
//...

loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
        mCallbackManager = CallbackManager.Factory.create();

//...

        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Toast.makeText(Ready.this, "Connected!", Toast.LENGTH_SHORT).show();

                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                Log.v("LoginActivity", response.toString());
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();

            }
            @Override
            public void onCancel() {
                // App code
                Log.v("LoginActivity", "cancel");
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.v("LoginActivity", exception.getCause().toString());
            }
        });

As I said before, I connect with facebook perfectly, but do not know how to assign, for example, email to a string and name to another string.

Thank you very much in advance.

Community
  • 1
  • 1
Sergio76
  • 3,835
  • 16
  • 61
  • 88

2 Answers2

4

If you meant how to get name and email from the GraphRequest, it's simply this:

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() { 
                        @Override 
                        public void onCompleted( 
                                JSONObject object,
                                GraphResponse response) {
                            // Application code 
                            try {
                                String id=object.getString("id");
                                String name=object.getString("name");
                                String email=object.getString("email");
                                String gender=object.getString("gender");
                                String birthday=object.getString("birthday");
                                //do something with the data here
                            } catch (JSONException e) {
                                e.printStackTrace(); //something's seriously wrong here
                            }
                        } 
                    }); 
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();

GraphResponse contains things like the HTTPConnect used, any error encountered, etc. Assuming everything goes as expected, only JSONObject object is needed to retrieve the data requested.

Kai
  • 15,284
  • 6
  • 51
  • 82
0

My working code for getting the profile information

    callbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    profile = Profile.getCurrentProfile();
                    if(profile != null){
                            fbUserId = profile.getId();
                            editor.putString("UserName",profile.getFirstName()+" "+profile.getLastName());                            
                            editor.putString("FbId",profile.getId()); 
                            editor.putString("ProfilePicture",profile.getProfilePictureUri(20,20).toString());                           
                            editor.commit();
                    }
                }

                @Override
                public void onCancel() {
                }

                @Override
                public void onError(FacebookException e) {

                }


            });

And you can track the profile changes here:

    profileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {

            if(currentProfile != null){
                fbUserId = currentProfile.getId();

                if(!sharedPreferences.contains("UserName")){
                    editor.putString("UserName",currentProfile.getFirstName()+" "+currentProfile.getLastName());
                }
                if(!sharedPreferences.contains("FbId")){
                    editor.putString("FbId",currentProfile.getId());
                }
                if(!sharedPreferences.contains("ProfilePicture")){
                    editor.putString("ProfilePicture",currentProfile.getProfilePictureUri(100,100).toString());
                }

                editor.commit();
            }
        }
    };
Deepika
  • 516
  • 4
  • 6