gpt4 book ai didi

java - 尝试将从图形 API 调用的字符串放入数据库 sq-lite,但数据库中的值为 null

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:10 26 4
gpt4 key购买 nike

我和我的团队已经这样做很多天了,但仍然不明白我想要做的是使用 Graph API Facebook 调用用户 ID(已经完成并通过使用日志检查它是否工作)并将该值放入 sq-lite 数据库中,但当我检查该值时它是 NULL。抱歉英语能力不好。这是一些代码

String userId, userName; //Global values
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
data = new Data(getApplicationContext());
SQLiteDatabase db = data.getWritableDatabase();
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,
GraphResponse response) {

userId = object.optString("id");
Log.d("id", userId);
}
});
request.executeAsync();
db.execSQL("INSERT INTO Information (userId) VALUES ('" + userId +"');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First exercise', 'Exercise', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Stronger', 'Exercise', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Body builder', 'Exercise', '100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First check-in', 'Check-in', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Go around', 'Check-in', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Become tourist', 'Check-in', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Traveller', 'Check-in','100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Wait! Is this the same place?', 'Check-in', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First time here', 'Todo', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('So busy', 'Todo', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Business man', 'Todo', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('What todo first', 'Todo', '100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First times Saving', 'Saving', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Money kids style', 'Saving', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Way to rich', 'Saving', '200');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Rich boy', 'Saving', '2000');");
db.close();
data.close();
loginState();
}

最佳答案

当数据库 userId 值尚未初始化时,您尝试写入该值。 GraphRequest.newMeRequest 在其他线程中执行,需要等待 onCompleted 被调用,然后写入 db。

  GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,
GraphResponse response) {

userId = object.optString("id");
Log.d("id", userId);
db.execSQL("INSERT INTO Information (userId) VALUES ('" + userId +"');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First exercise', 'Exercise', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Stronger', 'Exercise', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Body builder', 'Exercise', '100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First check-in', 'Check-in', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Go around', 'Check-in', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Become tourist', 'Check-in', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Traveller', 'Check-in','100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Wait! Is this the same place?', 'Check-in', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First time here', 'Todo', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('So busy', 'Todo', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Business man', 'Todo', '50');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('What todo first', 'Todo', '100');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('First times Saving', 'Saving', '1');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Money kids style', 'Saving', '10');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Way to rich', 'Saving', '200');");
db.execSQL("INSERT INTO Achievement (AName, AType, AScore) VALUES ('Rich boy', 'Saving', '2000');");
db.close();
data.close();
loginState();
}
});
request.executeAsync();

关于java - 尝试将从图形 API 调用的字符串放入数据库 sq-lite,但数据库中的值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36908154/

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