1

I'm using Facebook SDK 4.16.1 by using Profile I could get first name, last name, name and id my code is like this

public class LoginActivity extends Activity {

String id, fname, lname, email, name, gender, locale, verified;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;

 @Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.loginactivity);

FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
        }
    };

    LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList(Constants.FACEBOOK_PERMISSIONS));

    FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();

            FB_TOKEN=loginResult.getAccessToken().getToken();

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            response.getError();
                            Log.e("JSON:", object.toString());

                            try {
                                email = object.getString("email");
                                gender = object.getString("gender");
                                locale = object.optString("locale");
                                verified = object.optString("verified");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
            request.setParameters(parameters);
            request.executeAsync();


            id = profile.getId();
            fname = profile.getFirstName();
            lname = profile.getLastName();
            name = profile.getName();

            nextActivity(profile);
            Toast.makeText(getApplicationContext(), "Logging in...", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException e) {
        }
    };

    loginButton.registerCallback(callbackManager, callback);
}

 }

My constants looks like this

public static final String[] FACEBOOK_PERMISSIONS = new String[] {
                                                                "public_profile",
                                                                "user_friends",
                                                                "email" };

I'm trying to get the email, gender, locale and if its already verified. How will I get the other information? Mine are always null expect for the items that is get by Profile

Update

I update my GraphRequest like this

GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            // Application code
                            response.getError();
                            Log.e("JSON-RESULT:", object.toString());

                            JSONObject jsonObject = null;
                            try {
                                jsonObject = new JSONObject(object.toString());
                                email = jsonObject.getString("email");
                                gender = jsonObject.getString("gender");
                                locale = jsonObject.getString("locale");
                                verified = jsonObject.getString("verified");

                                Log.e(TAG, email + " This is the email");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,link,birthday,first_name,last_name,email,gender,verified,locale");
            request.setParameters(parameters);
            request.executeAsync();

In my Logcat it shows that I set the email and the others correctly but it doesn't go inside GraphRequest when you put breakpoints, and because of this my global variable email is still null. Even if in Log.e("JSON:", object.toString()); and Log.e(TAG, email + " This is the email"); it shows I get and set the values. I'm wondering how can I get the values of Bundle?

natsumiyu
  • 3,217
  • 7
  • 30
  • 54
  • Have you seen [this SO question](http://stackoverflow.com/questions/14542193/how-to-get-facebook-photo-full-name-gender-using-facebook-sdk-android) ? – Tim Biegeleisen Oct 17 '16 at 04:28

1 Answers1

1

Try using below code to fetch other info.....

 private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
               // Log.d("Login", "onSuccess");
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject me, GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                } else {
                                    email = me.optString("email");
                                    String id = me.optString("id");
                                    String email=me.optString("email");
                                    String name= me.optString("name");
                                    String gender= me.optString("gender");
                                    String verified= me.optBoolean("is_verified") + "";                                    
                                }
                            }
                        }).executeAsync();
            }


            @Override
            public void onCancel () {
              //  Log.d("Login", "onCancel");
            }

            @Override
            public void onError (FacebookException e){
              //  Log.d("Login", "onError " + e);
               }
       };
Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23