Facebook Android SDK 4.0.0未获取个人资料信息
在我的应用程序中登录到Facebook Android SDK 4.0.0未获取个人资料信息,android,facebook,facebook-login,facebook-android-sdk,Android,Facebook,Facebook Login,Facebook Android Sdk,在我的应用程序中登录到Facebook后,我在获取Profile.getCurrentProfile()时遇到问题。我正在使用Facebook Android SDK 4.0.0。登录后,我想获得用户名并在TextView中显示,但我得到的profile==null我不知道为什么。我有正确的应用程序ID和哈希键 这是我的代码: public class MyFragment extends Fragment { CallbackManager callbackManager;
Facebook
后,我在获取Profile.getCurrentProfile()
时遇到问题。我正在使用Facebook Android SDK 4.0.0。登录后,我想获得用户名并在TextView
中显示,但我得到的profile==null
我不知道为什么。我有正确的应用程序ID和哈希键
这是我的代码:
public class MyFragment extends Fragment {
CallbackManager callbackManager;
LoginButton loginButton;
FacebookCallback<LoginResult> loginResultFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Log.e("FB", String.valueOf(accessToken));
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
name.setText("Witam " + profile.getName());
Log.e("FB", "w");
}
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
};
TextView name;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment_facebook, container, false);
name = (TextView) view.findViewById(R.id.name);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, loginResultFacebookCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
公共类MyFragment扩展了片段{
CallbackManager CallbackManager;
登录按钮登录按钮;
FacebookCallback loginResultFacebookCallback=新建FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
AccessToken AccessToken=loginResult.getAccessToken();
Log.e(“FB”,String.valueOf(accessToken));
Profile Profile=Profile.getCurrentProfile();
if(profile!=null){
name.setText(“Witam”+profile.getName());
Log.e(“FB”、“w”);
}
}
@凌驾
公开作废{
//应用程序代码
}
@凌驾
public void onError(facebook异常){
//应用程序代码
}
};
文本视图名称;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getActivity().getApplicationContext());
callbackManager=callbackManager.Factory.create();
}
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.myfragment\u,container,false);
name=(TextView)view.findViewById(R.id.name);
返回视图;
}
@凌驾
已创建视图上的公共void(视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
loginButton=(loginButton)view.findViewById(R.id.login_按钮);
setReadPermissions(Arrays.asList(“public_profile”、“user_friends”);
setFragment(this);
registerCallback(callbackManager、loginResultFacebookCallback);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
}
当我在
Logcat
中检查令牌时,我得到AccessToken令牌:ACCESS\u Token\u已删除
Facebook登录
- 或
在登录后异步获取配置文件。如果要跟踪它,与此示例类似:对于我,代码Profile=Profile.getCurrentProfile();安装FB android应用程序时有效,卸载FB应用程序时会引发空指针异常,因此在该实例中,我的应用程序称为FB web应用程序(未返回配置文件)。这可能是FB sdk中存在的错误,而不是
loginresult.getAccessToken()
,您必须使用loginresult.getAccessToken().getToken()
。它返回包含访问令牌的字符串。我也有同样的问题,此错误是因为:
Profile.getProfile:是异步任务
我使用以下代码解决了问题:
LoginManager.getInstance().registerCallback(callbackManager,
新建FacebookCallback(){
@凌驾
成功时公开作废(最终登录结果登录结果){
if(Profile.getCurrentProfile()==null){
mProfileTracker[0]=新的ProfileTracker(){
@凌驾
更改当前配置文件时受保护的无效(配置文件、配置文件2){
Log.v(“facebook-profile”,profile2.getFirstName());
storeLogin(profile2.getName(),profile2.getProfilePictureUri(450450).toString(),loginResult.getAccessToken().getUserId());
openMainActivity(profile2.getName()、profile2.getProfilePictureUri(450450).toString()、loginResult.getAccessToken().getUserId());
loadingFace.disclose();
mProfileTracker[0]。停止跟踪();
}
};
mProfileTracker[0]。开始跟踪();
}
否则{
Profile Profile=Profile.getCurrentProfile();
storeLogin(profile.getName(),profile.getProfilePictureUri(450450).toString(),loginResult.getAccessToken().getUserId());
openMainActivity(profile.getName()、profile.getProfilePictureUri(450450).toString()、loginResult.getAccessToken().getUserId());
loadingFace.disclose();
}
}
@凌驾
公开作废{
....
致以最诚挚的问候感谢您的回答,但我实际上使用了此链接--来编写代码,他们说我应该仅通过getCurrentProfile方法获取配置文件,但我一直收到空值。我也有同样的问题。有人知道有什么解决方法吗?
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
if(Profile.getCurrentProfile() == null) {
mProfileTracker[0] = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
Log.v("facebook - profile", profile2.getFirstName());
storeLogin(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
openMainActivity(profile2.getName(), profile2.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
loadingFace.dismiss();
mProfileTracker[0].stopTracking();
}
};
mProfileTracker[0].startTracking();
}
else {
Profile profile = Profile.getCurrentProfile();
storeLogin(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
openMainActivity(profile.getName(), profile.getProfilePictureUri(450, 450).toString(), loginResult.getAccessToken().getUserId());
loadingFace.dismiss();
}
}
@Override
public void onCancel() {
....