FB Android-SDK:如何等待,直到请求的回调完成



我正试图等待Request的回调函数的onCompleted()完成。然而,RequestAsyncTaskget()方法总是在这种情况之前返回。因此,在我的程序的进一步陈述中出现了问题。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。我该如何解决这个问题?

你应该把这部分代码:

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
    }    
}

相关内容

  • 没有找到相关文章

最新更新