Facebook用户 - Android中的用户名,电子邮件,年龄,位置,性别,个人资料照片

时间:2016-06-13 09:06:58

标签: android facebook

我试图通过facebook sdk登录facebook按钮来获取这些值。这有什么问题 - 它没有做任何事情。当我点击登录按钮时,它只是加载一点而没有任何反应。我也没有看到任何印刷品。

public class MainActivity extends AppCompatActivity {

private LoginButton loginButton;
private CallbackManager callbackManager;
private ImageView profilePicture;
private TextView tvNameAge, tvEmail, tvLocation, tvGender;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    AppEventsLogger.activateApp(this);
    setContentView(R.layout.activity_main);



    profilePicture = (ImageView) findViewById(R.id.ivProfile);
    tvNameAge = (TextView) findViewById(R.id.tvNameAge);
    tvEmail = (TextView) findViewById(R.id.tvEmail);
    tvLocation = (TextView) findViewById(R.id.tvLocation);
    tvGender = (TextView) findViewById(R.id.tvGender);

    loginButton = (LoginButton) findViewById(R.id.login_button);

    loginButton.setReadPermissions(Arrays.asList(
            "public_profile", "email", "user_birthday", "user_friends", "user_location"));

    callbackManager = CallbackManager.Factory.create();

    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            System.out.println("ONSUCCESS");
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {

                            try {
                                // Application code
                                String name = object.getString("first_name");
                                String email = object.getString("email");
                                String birthday = object.getString("birthday"); // 01/31/1980 format
                                String location = object.getString("location");
                                String gender = object.getString("gender");
                                String id = object.getString("id");

                                tvNameAge.setText(name + ", " + birthday);
                                tvEmail.setText(email);
                                tvGender.setText(gender);
                                tvLocation.setText(location);


                                Bitmap bitmap = getFacebookProfilePicture(id);
                                profilePicture.setImageBitmap(bitmap);

                            }catch (Exception e){}

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender,birthday");
            request.setParameters(parameters);
            request.executeAsync();


        }

        @Override
        public void onCancel() {
            // App code
            System.out.println("ONCANCEL");

        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            System.out.println("ONERROR");
        }
    });
}

public static Bitmap getFacebookProfilePicture(String userID){

    Bitmap bitmap = null;

    try {
        URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
        bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }


    return bitmap;
}

}

1 个答案:

答案 0 :(得分:0)

我错过了这段代码:

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