1

我在 android 中使用 Fragments 并将 facebook 登录与其集成,以获取用户详细信息。

public void postOnfacebook() {
        Session.openActiveSession(getActivity(), this, true,
                new StatusCallback() {

                    @Override
                    public void call(Session session, SessionState state,
                            Exception exception) {
                        if (session.isOpened()) {
                            Request.executeMeRequestAsync(session,
                                    new GraphUserCallback() {

                                        @Override
                                        public void onCompleted(GraphUser user,
                                                Response response) {
                                            if (user != null) {
                                                id = user.getId();
                                                name = user.getName();

                                                gender = user
                                                        .getProperty("gender") == null ? "male"
                                                        : user.getProperty(
                                                                "gender")
                                                                .toString();


                                            }

                                    }
                                });
                    }

                }
            });
}

fragment根据文档,我还包含以下方法

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(getActivity(), requestCode,
                resultCode, data);
}

我在 中尝试过相同的代码Activity,它在那里工作,但在Fragment. 根据文档,我已经包含LoginActivity在里面AndroidManifest.xmlmetadatefor中。facebookapplicationId

你能建议我在哪里出错,以防万一Fragment

4

2 回答 2

0

我想出了解决方案,上面的代码实际上是正确的,我做错了什么,因为它不起作用。我更正了上面的代码有效

于 2013-03-07T12:22:33.837 回答
0

据我了解,facebook sdk 不支持片段。所以onActivityForResult不会被触发,这个要移到fragments中Parent Parent FragmentActivity应该有如下代码;这也会更新片段中的会话,您可以处理片段中的会话。

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("OnActivityResult fragment Parent");
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
    }

在片段中添加类似于以下内容的内容。使用 getActivity() 获取片段。session.isOpened() 会在执行 parentFragmentActivity 的 onActivityResult 时自动触发。

btnFacebook.setOnClickListener(new View.OnClickListener() {         
    @Override
    public void onClick(View v) {   
        Session.openActiveSession(getActivity(), true, new Session.StatusCallback() {
                    @Override
                    public void call(final Session session, SessionState state,
                            Exception exception) {
                        System.out.println("Session :"+session.getState());
                        if (session.isOpened()) {
                            Request.executeMeRequestAsync(session,
                                    new GraphUserCallback() {

                                        @Override
                                        public void onCompleted(GraphUser user,
                                                Response response) {
                                            if (user != null) {
                                                System.out.println("User ID "+user.getId());
                                                System.out.println("User Name "+user.getFirstName()+" "+user.getLastName());;
                                                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                                                    // callback after Graph API response with user object
                                                    @Override
                                                    public void onCompleted(GraphUser user, Response response) {
                                                      if (user != null) {
                                                          System.out.println(user.getName());
                                                          System.out.println("facebook user id" + user.getId());
                                                         System.out.println(user.asMap().get("email").toString());
                                                         // Session.OpenRequest open = new Session.OpenRequest(Login)
                                                      }
                                                    }
                                                  });
                                            }

                                    }
                                });
                    }

                }
            });
    }
});
于 2014-03-07T07:52:50.450 回答