新新新新旗新新新旗新新新新旗新新旗新新旗新新旗新新旗旗新新旗旗新新旗新旗旗新新旗新旗旗新新旗新新旗旗 [英] Facebook Graph in login onSuccess execute and wait

查看:194
本文介绍了新新新新旗新新新旗新新新新旗新新旗新新旗新新旗新新旗旗新新旗旗新新旗新旗旗新新旗新旗旗新新旗新新旗旗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户登录后收集用户名和个人资料图片。我需要在登录过程中执行此操作,我尝试在 onSuccess Methode来自 FacebookCallback< LoginResult> 但是失败了。

I need to collect the user name and the profile picture after the user logged in. I need to do that in the login process, i tried that in the onSuccess Methode from the FacebookCallback<LoginResult> but it failed.

我的代码:

btnFbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {


        @Override
        public void onSuccess(LoginResult loginResult) {
            final String userId = loginResult.getAccessToken().getUserId();
            final String authToken = loginResult.getAccessToken().getToken();

            // Facebook Name, Profile Picture
            /*GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {

                            try {
                                userName = object.getString("name");
                                String url = object.getString("picture");

                                URL facebookProfileURL = new URL(url);
                                Bitmap profPict = BitmapFactory.decodeStream(facebookProfileURL.openConnection().getInputStream());
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                profPict.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                                byte[] b = baos.toByteArray();

                                profilePicAsString = Base64.encodeToString(b, Base64.DEFAULT);

                                facebookAuthObject.setUserId(userId);
                                facebookAuthObject.setAuthToken(authToken);
                                facebookAuthObject.setName(userName);
                                facebookAuthObject.setProfilePictureAsString(profilePicAsString);
                            } catch (JSONException | IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "name, picture.width(150).height(150)");
            request.setParameters(parameters);
            request.executeAsync();*/


        }

        @Override
        public void onCancel() {
            Log.i(TAG, "User cancel");
        }

        @Override
        public void onError(FacebookException e) {
            Log.e(TAG, e.getLocalizedMessage());
        }
    });

}

我需要像 executeAndWait 但是这是不可能的,因为我在主线程。

I need something like the executeAndWait but this is impossible because I'm on the main thread.

有了这个问题,如果用户离开活动太快我没有什么,因为异步。

With this I have the problem that if the user leave the activity too fast i have nothing, because of the async.

有人有一个想法,我可以阻止ui线程吗?还是其他解决方案收集名称和个人资料图片?

Have anybody an idea how i can block the ui thread? Or other solutions for collecting the name and the profile picture?

提前谢谢

推荐答案

通过这个可以获得详细信息从Facebook

Through this you can get the details from facebook

FacebookSdk.sdkInitialize(this.getApplicationContext());

FacebookSdk.sdkInitialize(this.getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
            }
        };
        accessToken = AccessToken.getCurrentAccessToken();

        final LoginManager loginManager = LoginManager.getInstance();

        loginManager.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {

                                    @Override
                                    public void onCompleted(
                                            JSONObject object,
                                            GraphResponse response) {

                                        Log.v(TAG, "facebook login response: " + object.toString());
                                        try {
                                            email=object.getString("email");


                                            loginManager.logOut();
                                            startActivity(new Intent(LoginActivity.this, MudraMainActivity.class));
                                            finish();

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


                                    }
                                });

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

                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onError(FacebookException exception) {
                    }
                });

        loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends", "email"));

这篇关于新新新新旗新新新旗新新新新旗新新旗新新旗新新旗新新旗旗新新旗旗新新旗新旗旗新新旗新旗旗新新旗新新旗旗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆