获取ProfilePictureView Android Studio的数据

时间:2016-08-14 15:03:08

标签: java android facebook android-studio facebook-sdk-4.0

我试图从Facebook上显示用户数据。

我在activity中创建了一个ProfilePictureView和userName。 (我为此设置了id)

接下来我想发布java类的数据。

当“按钮”成功登录时,我获得了id,这个数据被发布到活动中。 但那不行......

功能:

protected void getLoginDetails(LoginButton login_button){
        // Callback registration
        login_button.registerCallback(callbackManager, new FacebookCallback <LoginResult>() {
            @Override
            public void onSuccess(LoginResult login_result) {
                    Intent intent = new Intent(MainActivity.this,HomePage.class);
                    startActivity(intent);

                    ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture);
                    profilePictureView.setCropped(true);
                    profilePictureView.setProfileId(login_result.getId());

                    TextView Name = (TextView) findViewById(R.id.userName);
                    Name.setText(login_result.getName());

            }

            @Override
            public void onCancel() {
                // code for cancellation
            }
            @Override
            public void onError(FacebookException exception) {
                //  code to handle error
            }
        });
    }

的活动:

<com.facebook.login.widget.ProfilePictureView
    android:id="@+id/userProfilePicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal" />

<TextView
    android:id="@+id/userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textColor="#333"
    android:textSize="18sp" />

1 个答案:

答案 0 :(得分:0)

下面是我从FB获取一些信息的示例代码,记得声明权限并从onComplete方法获取结果:

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,
Arrays.asList("user_friends","user_location","user_birthday","user_likes","user_photos"));

GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
String strName = object.optString("name");
name.setText(strName);
String strBirthday = object.optString("birthday");
birthday.setText(strBirthday);
String strLocation = object.optString("location");
try {
JSONObject objLocation = new JSONObject(strLocation);
String locationName = objLocation.optString("name");
location.setText(locationName);
} catch (JSONException e) {
e.printStackTrace();
}
profilePictureView.setProfileId(object.optString("id"));

}
}
);