Android Cant从Facebook SDK 4获取所有用户信息

时间:2017-04-13 11:12:25

标签: android facebook-sdk-4.x

我正试图从Facebook SDK获取所有信息。实际上这段代码正在运行,但我遇到了问题。此代码无法从所有Facebook帐户获取所有信息。当我从我的帐户登录时,它正确地获取了我的所有信息,但是从其他2个不同的帐户中它没有全部并且返回null。他们在他们的个人资料中有电子邮件或城市信息,甚至他们给予权限我的代码不接受这些信息。

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_login);
       user = PrefUtils.getCurrentUser(LoginActivity.this);


        btnLogout = (TextView) findViewById(R.id.btnLogout);
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrefUtils.clearCurrentUser(LoginActivity.this);

                LoginManager.getInstance().logOut();


                Intent i = new Intent(LoginActivity.this, LoginActivity.class);
                startActivity(i);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();


        callbackManager = CallbackManager.Factory.create();


        loginButton = (LoginButton) findViewById(R.id.login_button);
        profileImage = (ImageView) findViewById(R.id.profileImage);
        loginButton.setReadPermissions("public_profile", "email", "user_friends", "user_birthday", "user_location");

        btnLogin = (TextView) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog = new ProgressDialog(LoginActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();

                loginButton.performClick();
                loginButton.setPressed(true);
                loginButton.invalidate();
                loginButton.registerCallback(callbackManager, mCallBack);
                loginButton.setPressed(false);
                loginButton.invalidate();

            }
        });
    }

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


    private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            progressDialog.dismiss();


            GraphRequest request = GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {

                            Log.e("response: ", response + "");
                            try {
                                user = new User();

                                if(object.getString("id").toString() != null){
                                    user.facebookID = object.getString("id").toString();
                                }
                                else
                                {
                                    user.facebookID = "empty";
                                }

                                if(object.getString("email").toString() != null){
                                    user.email = object.getString("email").toString();
                                }
                                else
                                {
                                    user.email = "empty";
                                }

                                if(object.getString("name").toString() != null){
                                    user.name = object.getString("name").toString();
                                }
                                else
                                {
                                    user.name = "empty";
                                }

                                if(object.getString("gender").toString() != null){
                                    user.gender = object.getString("gender").toString();
                                }
                                else
                                {
                                    user.gender = "empty";
                                }

                                if(object.getString("birthday").toString() != null){
                                    user.birthday = object.getString("birthday").toString();
                                }
                                else
                                {
                                    user.birthday = "empty";
                                }

                                if (object.getString("location").toString() != null) {
                                    user.locale =object.getJSONObject("location").getString("name").toString();
                                } else {
                                    user.locale = "empty";
                                }




                                id.setText(user.facebookID);
                                name.setText(user.name);
                                mail.setText(user.email);
                                gender.setText(user.gender);
                                birthday.setText(user.birthday);
                                location.setText(user.locale);


                                new AsyncTask<Void, Void, Void>() {
                                    @Override
                                    protected Void doInBackground(Void... params) {
                                        URL imageURL = null;
                                        try {
                                            imageURL = new URL("https://graph.facebook.com/" + user.facebookID + "/picture?type=large");
                                        } catch (MalformedURLException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());

                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }



                                        return null;

                                    }

                                    @Override
                                    protected void onPostExecute(Void aVoid) {
                                        super.onPostExecute(aVoid);
                                        profileImage.setImageBitmap(bitmap);
                                    }
                                }.execute();

                                //PrefUtils.setCurrentUser(user, LoginActivity.this);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Toast.makeText(LoginActivity.this, cevap, Toast.LENGTH_LONG).show();
                            //Toast.makeText(LoginActivity.this, "welcome " + user.name, Toast.LENGTH_LONG).show();
                        }
                    });

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

        @Override
        public void onCancel() {
            progressDialog.dismiss();
        }

        @Override
        public void onError(FacebookException e) {
            progressDialog.dismiss();
        }
    };

0 个答案:

没有答案