gpt4 book ai didi

android - FB Android-SDK : How to wait until callback of request is completed

转载 作者:行者123 更新时间:2023-11-30 02:51:27 24 4
gpt4 key购买 nike

我试图等到 Request 的回调函数的 onCompleted() 完成。然而,RequestAsyncTask 的方法get() 总是在这种情况之前返回。因此,在我的程序的进一步陈述中会出现问题。 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
}
}

关于android - FB Android-SDK : How to wait until callback of request is completed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com