gpt4 book ai didi

java - 如何检查数据是否已成功插入 Firebase 数组中

转载 作者:行者123 更新时间:2023-12-02 01:11:10 25 4
gpt4 key购买 nike

我正在使用以下代码在 Firebase 数据库中从 json 响应插入一些数据到数组中

JSONArray arr = new JSONArray(result);
String[] stocks = new String[arr.length()];

for(int i=0;i<arr.length();i++){

JSONObject obj = arr.getJSONObject(i);
mDatabase= FirebaseDatabase.getInstance().getReference().child("books");
atabaseReference newBid=mDatabase.push();
newBid.child("usr_id").setValue(obj.getString("user_id"));
newBid.child("usr_fullNme").setValue(obj.getString("first_name")+" "+obj.getString("last_name"));
newBid.child("usr_mobile").setValue(obj.getString("user_mobile"));
newBid.child("usr_avatr").setValue(obj.getString("src"));
}

如何检查以上操作是否成功

最佳答案

您可以使用Hashmap并执行以下操作:

Map<String, Object> userValues = new HashMap<>();
userValues.put("usr_id", obj.getString("user_id"));
userValues.put("usr_fullNme",obj.getString("first_name")+" "+obj.getString("last_name"));
userValues.put("usr_mobile", obj.getString("user_mobile"));
userValues.put("usr_avatr", obj.getString("src"));

然后使用setValue():


mDatabase= FirebaseDatabase.getInstance().getReference().child("books");
String newBid = mDatabase.push().getKey();
mDatabase.child(newBid).setValue(userValues, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
System.out.println(databaseError);
}
});

来自docs :

public void setValue (Object value, DatabaseReference.CompletionListener listener)

Set the data at this location to the given value. Passing null to setValue() will delete the data at the specified location. The native types accepted by this method for the value correspond to the JSON types:

  • Boolean
  • Long
  • Double
  • String
  • Map
  • List

关于java - 如何检查数据是否已成功插入 Firebase 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59375931/

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