1

我试图等到onCompleted()我的回调函数 aRequest完成。然而,总是在此之前返回的方法get()就是RequestAsyncTask这种情况。因此,在我的程序的进一步陈述中出现了问题。println输出总是在数据库查询之后显示并且不相等null。这是我的代码:

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            // show authendicated ui
            Log.i(TAG, "Logged in...");
            try {
                Request.newMeRequest(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            MyApplication.userId = user.getId();
                            System.out.println("User id: " + MyApplication.userId);
                        }

                    }
                }).executeAsync().get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);

这会导致我的数据库查找null为 uid。我怎么解决这个问题?

4

1 回答 1

1

你应该把这部分代码:

boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);

在您的请求回调中:

 @Override
public void onCompleted(GraphUser user, Response response) {
    if (user != null) {
        MyApplication.userId = user.getId();
        System.out.println("User id: " + MyApplication.userId);

        boolean isNewUser = !MyApplication.dbConnection.isUidRegistered(MyApplication.userId);
        // here you should continue your work
    }    
}
于 2014-06-06T21:18:06.060 回答