Facebook登录android

时间:2014-04-09 12:40:56

标签: android facebook facebook-graph-api

我正在尝试打开Facebook画廊,如果登录在Facebook其他我用android sdk在android中登录facebook登录,我写了这段代码:

Session.openActiveSession(FbGallery.this, true, new Session.StatusCallback() {
    @SuppressWarnings("deprecation")
    @Override
    public void call(final Session session, SessionState state, Exception exception) {

        System.out.println("Session:::"+session.toString());

        if(session.isOpened()){
            session.requestNewPublishPermissions(
                    new Session.NewPermissionsRequest(FbGallery.this, "user_photos"));
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    Toast.makeText(getApplicationContext(), "Session is opened", Toast.LENGTH_SHORT).show();
                }
            });
        }
        else{
            Toast.makeText(getApplicationContext(), "Gone to else", Toast.LENGTH_SHORT).show();
        }
    }
});

它工作得很好并且如果没有登录则获取登录页面但如果登录则会以递归方式运行代码,即。 toast消息'会话被打开'在这里。

是什么原因。我无法理解......

2 个答案:

答案 0 :(得分:1)

private void onClickRequest()     {

    if (this.session.isOpened() && session!=null) 
    {
        shareDialog=new ProgressDialog(MainActivity.this);
        shareDialog.setTitle("Post ON Wall");
        shareDialog.setMessage("Wait..........");
        shareDialog.setCancelable(false);

        System.out.println("Already Logged In");

        if (session.isOpened()) {
            // make request to the /me API
            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.getId()+user.getName()+"........................");
                id=user.getId();
                Toast.makeText(getApplicationContext(), session.getAccessToken(), 100).show();
            }
            }
            });
            }
        //postPhoto();
    } else {
        StatusCallback callback = new StatusCallback() {
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (exception != null) {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Failed Login")
                            .setMessage(exception.getMessage())
                            .setPositiveButton("Ok", null)
                            .show();
                    MainActivity.this.session = createSession();
                }
            }
        };
        pendingRequest = true;
        MainActivity.this.session = createSession();


        this.session.openForRead(new Session.OpenRequest(this)
        .setCallback(callback).setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO).setPermissions(""));




    }

答案 1 :(得分:0)

if (session.isOpened())
 {
 Toast.makeText(getApplicationContext(), "Already Login",500).show();
 onClickRequest();
 } 
else 
  {
  onClickRequest();
     session = createSession();
  }