我正在尝试使用Firebase实时数据库遵循Firebase的在线指南,但由于我的权限被拒绝,我无法获得数据库的任何更新。我不确定我做错了什么。我甚至将规则设置为读取和写入为真。
onClickListener中的代码如下:
Button vDetails = (Button) v.findViewById(R.id.enter_vechile_details);
vDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
myRef.child("Customers").child("Drivers").setValue("Name").addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "signUp: set customer name" + e);
e.printStackTrace();
}
});
getFragmentManager().beginTransaction().replace(R.id.fragment_container, new SignUpCustomerFragment()).commit();
}
});
这是在片段中,按钮在开始更换片段的事务之前首先完成这些语句。
这是Firebase中的规则:
{
"rules": {
".read": true,
".write": true
}
}
由于我是JSON的新手,我还在“.write”结束时尝试使用逗号
{
"rules": {
".read": true,
".write": true,
}
}
这是堆栈跟踪:
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: com.google.firebase.database.DatabaseException: Firebase Database error: Permission denied
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at com.google.firebase.database.DatabaseError.toException(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at com.google.android.gms.internal.zzall$1.onComplete(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at com.google.android.gms.internal.zzahq$20.run(Unknown Source)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at android.os.Looper.loop(Looper.java:168)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5885)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at java.lang.reflect.Method.invoke(Native Method)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
09-10 04:32:46.274 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
09-10 04:32:46.276 16594-16594/com.buildingstories.sabbib.xpose W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
我一整天都在这里,因为我遵循了Firebase指南,所以我不知道出了什么问题。
答案 0 :(得分:0)
您需要为" drive"创建一个孩子。并按下面给出的代码推送Customer类,并记住在Customer Class中创建Empty Constructor:
>>> value1 = "A"
>>> value2 = "B"
>>> d = {"set1": {"A", "B", "C"}, "set2": {"D", "E", "F"}, "set3":{"A", "D", "C"}}
>>> [k for k, v in d.items() if value1 in v and value2 in v]
['set1']
答案 1 :(得分:0)
您已在子级中为更新值设置主题
在构造函数中传递值不要设置。
Customer customer = new Customer(fullName.getText().toString()); // Pass values here in constructor
//customer.setFirstName(fullName.getText().toString());
myRef.child("Drivers").setValue(customer);
答案 2 :(得分:0)
您应该使用HashMap
来推送您的值并添加onFailureListener
,以便了解您获得的error
:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Drivers");
Customer customer = new Customer();
customer.setFirstName(fullName.getText().toString());//fullName is EditText
Map<String,Object> map = new HashMap<String,Object>();
map.add("drive",customer);
myRef.push().setValue(map).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
注意:确保Customer
类有空构造函数或Firebase
无法Serialize
您的课程。
答案 3 :(得分:0)
下面的代码会将Drivers类设置为“drive”
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
db.child("Drivers").setValue("drive");
请注意,push()和setValue()是不同的。您可以阅读有关在实时Firebase数据库中保存数据的更多信息here