1

我已将我的应用与 facebook 登录集成。这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Session.openActiveSession(this, true, new Session.StatusCallback() {

      public void call(Session session, SessionState state, Exception exception) {
        if (session.isOpened()) {


          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {


            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {
                TextView t = (TextView) findViewById(R.id.text);
                t.setText("Hello " + user.getName() + "!");
              }
            }
          });
        }
      }
    });
  }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }`

在设备模拟器中,此代码有效,但是当我在设备中尝试时,应用程序会收到此消息:因此,当我单击确定时,未使用用户名设置 textView。为什么?

4

1 回答 1

0

我需要额外的阅读权限,所以我的代码如下所示:

    OpenRequest req = new OpenRequest(getActivity()).setCallback(new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {                
            if (session.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            //do login stuff
                        } else {
                            //show error message

                        }
                    }
                });
            } else {
                if (exception != null) {
                    //show error
                }
            }
        }
    }); 
    req.setPermissions(Arrays.asList("email", "user_location"));
    Session session = new Builder(getActivity()).build();
    Session.setActiveSession(session);
    session.openForRead(req);

这是在 Fragment 里面,所以你可以getActivity()this. 使用正确的 onActivityResult() 方法,我对 FB 没有任何问题。

于 2013-02-25T16:34:47.260 回答