После входа в Facebook профиль пуст

Я искал StackOverflow, но не нашел ответа. Мне нужно имя и фамилия профиля из Facebook после входа в систему. Профиль всегда нулевой после входа в систему. Как решить эту проблему?

Вот мой код:

private CallbackManager callbackManager;
private TextView textView;

private AccessTokenTracker accessTokenTracker;
private ProfileTracker mProfileTracker;

private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {

    @Override
    public void onSuccess(LoginResult loginResult) {
        mProfileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                Log.v("facebook - profile", profile2.getFirstName());
                mProfileTracker.stopTracking();
            }
        };
        mProfileTracker.startTracking();
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);
        register(profile);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {

    }
};

public FBLoginFragment() {

}


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

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

        }
    };

    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
            displayMessage(newProfile);
        }
    };

    accessTokenTracker.startTracking();
    mProfileTracker.startTracking();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
    textView = (TextView) view.findViewById(R.id.textView);

    loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, callback);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (callbackManager.onActivityResult(requestCode, resultCode, data))
        return;
}

private void displayMessage(Profile profile){
    if(profile != null){
        textView.setText(profile.getName());
    }
}

private void register(Profile profile){
    new Register().execute(profile.getFirstName(), profile.getLastName());
}

@Override
public void onStop() {
    super.onStop();
    accessTokenTracker.stopTracking();
    mProfileTracker.stopTracking();
}
@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    displayMessage(profile);
}
}

В манифесте у меня есть <uses-permission android:name="android.permission.INTERNET"/>


person christian russo    schedule 06.07.2015    source источник
comment
Я исправил некоторые опечатки, отформатировал и переформулировал предложения для лучшей читабельности. Пожалуйста, будьте ясны и объясните, что именно вы хотите, чтобы произошло. Теперь неясно, пожалуйста, добавьте больше деталей.   -  person Ram    schedule 06.07.2015


Ответы (1)


После успешного входа в Facebook иногда обновление Profile занимает некоторое время, вам нужно дождаться вызова onCurrentProfileChanged, чтобы получить обновленное Profile.

Попробуйте изменить:

@Override
public void onSuccess(LoginResult loginResult) {
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
            Log.v("facebook - profile", profile2.getFirstName());
            mProfileTracker.stopTracking();
        }
    };
    mProfileTracker.startTracking();
    AccessToken accessToken = loginResult.getAccessToken();
    Profile profile = Profile.getCurrentProfile();
    displayMessage(profile);
    register(profile);
}

to

@Override
public void onSuccess(LoginResult loginResult) {
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
            Log.v("facebook - profile", profile2.getFirstName());
            mProfileTracker.stopTracking();

            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();//Or use the profile2 variable
            displayMessage(profile);
            register(profile);
        }
    };
    mProfileTracker.startTracking();
}
person Marco Batista    schedule 06.07.2015