簡體   English   中英

Facebook用戶-Android中的姓名,電子郵件,年齡,位置,性別,個人資料照片

[英]Facebook user - name, email, age, location, gender, profile pic in Android

我正在嘗試從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;
}

}

我錯過了這段代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM