gpt4 book ai didi

android - 从 Android 上的 Facebook SDK 获取配置文件数据始终返回 Null。为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:54 25 4
gpt4 key购买 nike

我刚刚了解了 facebook SDK on android。我已经在 stackoverflow 和 facebook developer guide 上搜索登录,但是当从 facebook sdk 获取个人资料数据时我仍然卡住了。我尝试从以下位置实现解决方案:unable get profileGet email ,但仍然卡住了。

这是我的代码:

    public class HomeLoginActivity extends Activity {
LoginButton btnFacebook;
CallbackManager callbackManager = CallbackManager.Factory.create();
ProfileTracker profTrack;
AccessTokenTracker accessTokenTracker;
TextView welcomeText;
FacebookCallback<LoginResult> mFacebookCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_home_login);
welcomeText = (TextView) findViewById(R.id.welcome_id);
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
// App code
Log.d("current token", "" + currentAccessToken);

//}
}
};
profTrack = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(
Profile oldProfile,
Profile currentProfile) {
// App code
Log.d("current profile", "" + currentProfile);
welcomeText.setText(constructWelcomeMessage(currentProfile));
}

};
mFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
welcomeText.setText(constructWelcomeMessage(profile));

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");
Stringbirthday=object.getString("birthday");

//do something with the data here
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException e) {

}
};

accessTokenTracker.startTracking();
profTrack.startTracking();

//Button Facebook
btnFacebook = (LoginButton) findViewById(R.id.btnFacebook);
btnFacebook.setReadPermissions("public_profile", "user_friends");
btnFacebook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions((Activity) v.getContext(),Arrays.asList("public_profile", "user_friends"));
}
});
btnFacebook.registerCallback(callbackManager, mFacebookCallback);

}
// ennd on create
private String constructWelcomeMessage(Profile profile) {
StringBuffer stringBuffer = new StringBuffer();
if (profile != null) {
stringBuffer.append("Welcome " + profile.getName());
}
else {
stringBuffer.append("NULL Profile");
}
return stringBuffer.toString();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);

}
@Override
public void onResume() {
super.onResume();
AccessToken.getCurrentAccessToken();
Log.d("resume current token", "" + AccessToken.getCurrentAccessToken());
Profile.fetchProfileForCurrentAccessToken();
}

@Override
public void onStop() {
super.onStop();
profTrack.stopTracking();
accessTokenTracker.stopTracking();
}

@Override
public void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
profTrack.stopTracking();
}
}

还有我的日志猫:

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
at com.twiscode.gimme.HomeLoginActivity$3$1.onCompleted(HomeLoginActivity.java:100)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:298)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1246)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

试试这个示例代码来获取个人资料信息

loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code

// login ok get access token
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,
GraphResponse response) {

if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk
.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

System.out
.println("AccessToken.getCurrentAccessToken()"
+ AccessToken
.getCurrentAccessToken()
.toString());
Profile.getCurrentProfile().getId();
Profile.getCurrentProfile().getFirstName();
Profile.getCurrentProfile().getLastName();
Profile.getCurrentProfile().getProfilePictureUri(50, 50);
//String email=UserManager.asMap().get(“email”).toString();
}
}
});
request.executeAsync();
/* Bundle parameters = new Bundle();
parameters
.putString("fields",
"id,firstName,lastName,name,email,gender,birthday,address");
request.setParameters(parameters);



Intent loginintent = new Intent(getActivity(),
EditProfile.class);
startActivity(loginintent);
System.out.println("XXXX " + getId());
*/
makeJsonObjReq();
}

@Override
public void onCancel() {
// App code
}

@Override
public void onError(FacebookException exception) {
// App code
}

});

return view;

关于android - 从 Android 上的 Facebook SDK 获取配置文件数据始终返回 Null。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com