我正试图等待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
}
}