Facebook登录后,个人资料为空

时间:2015-07-06 16:53:22

标签: android facebook facebook-login

我搜索了StackOverflow,但我找不到答案。登录后我需要Facebook的个人资料的名字和姓氏。登录后,配置文件始终为null。如何解决这个问题?

这是我的代码:

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"/>

1 个答案:

答案 0 :(得分:2)

成功登录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);
}

@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();
}