[英]Permission Denied to Firebase RealTime Database
I'm trying to use Firebase Realtime database following Firebase's guide online but I can't get any updates on the database as my permission is being denied. 我正在尝试在Firebase指南在线使用Firebase实时数据库,但由于我的权限被拒绝,我无法获得数据库的任何更新。 I am not sure what I'm doing wrong.
我不确定我做错了什么。 I've even set the rules to read and write to true.
我甚至将规则设置为读取和写入为true。
The code inside onClickListener is below: 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();
}
});
This is inside a fragment and the button completes those statements first before it begins the transaction for the fragment replacement. 这是在片段内部 ,按钮在开始片段替换的事务之前首先完成这些语句。
This is the rules in Firebase: 这是Firebase中的规则:
{
"rules": {
".read": true,
".write": true
}
}
As I am new to JSON, I've also tried with a comma at the end of ".write" 由于我是JSON的新手,我还在“.write”结束时尝试使用逗号。
{
"rules": {
".read": true,
".write": true,
}
}
This is the stacktrace: 这是堆栈跟踪:
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
I've been at this all day and I have no idea what's wrong since I followed the Firebase guide. 我一整天都在这里,因为我遵循了Firebase指南,所以我不知道出了什么问题。
You need to create a child for "drive" and push the Customer class as given below code and remember to create Empty Constructor in your Customer Class: 您需要为“drive”创建一个子项,并按下面的代码推送Customer类,并记住在Customer Class中创建Empty Constructor:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Drivers");
Customer customer = new Customer();
customer.setFirstName(fullName.getText().toString());//fullName is EditText
myRef.child("drive").push().setValue(customer);
You have set topic in child for update value 您已在子项中设置主题以获取更新值
Pass Value in constructor don't set. 构造函数中的传递值不设置。
Customer customer = new Customer(fullName.getText().toString()); // Pass values here in constructor
//customer.setFirstName(fullName.getText().toString());
myRef.child("Drivers").setValue(customer);
You should use HashMap
to push your values as and add a onFailureListener
so you can see what error
you are getting: 你应该使用
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();
}
});
NOTE : Make sure Customer
class has an empty constructor or Firebase
won't be able to Serialize
your class. 注意 :确保
Customer
类具有空构造函数,否则Firebase
将无法Serialize
您的类。
Below code will set the Drivers class to "drive" . 下面的代码将Drivers类设置为“drive”。
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
db.child("Drivers").setValue("drive");
note that push() and setValue() are different. 请注意,push()和setValue()是不同的。 you can read more about saving data on your Realtime Firebase Database here
您可以在此处详细了解如何在Realtime Firebase数据库中保存数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.