基本的Firebase数据库保存 [英] Basic Firebase database save
问题描述
任何人看下面这个是正确的答案,我已经正确地设置了一切。
我仍然不知道问题是什么。
我是用facebook登录的,用它来创建一个firebaseuser对象。
然后发送下面的测试数据。
我发现这是一个间歇性的问题。从设备上卸载应用程序并重新部署通常会解决问题。
非常奇怪
实际上,我正在努力让这个例子起作用。
设置值....
FirebaseDatabase数据库= FirebaseDatabase.getInstance();
Log.d(TAG,db ref:+ database.getReference());
DatabaseReference myRef = database.getReference(message);
myRef.setValue(Hello,World!);
当我进入数据库控制台时,无法看到我发送的值。
我的日志正在返回一个对数据库的引用。
我也设置我的规则作为公共安全的一面。
任何想法我做错了什么?
其实你正在引用一个逻辑上不存在的孩子'消息',
如果要命名数据库的根目录,只需按照以下步骤操作即可:
DatabaseReference myRef1 = FirebaseDatabase.getInstance()。getReference(); //获取根引用
myRef1.setValue(Hello,World!);
如果你想在 database code>,确保你的
数据库中有一个孩子
DatabaseReference myRef1 = FirebaseDatabase.getInstance()。getReference(); //获取根引用
DatabaseReference myRef = myRef1.child(message); //写下你的子引用(如果有的话)
myRef.setValue(Hello,World!);
它会覆盖您的孩子的值到'Hello World'
转到 Firebase控制台
并创建一个项目并为其指定一个有效的packageName使用。和下载google-service.json 文件,然后将此文件添加到应用文件夹
中,方法是将您的Android视图更改为项目视图。
$ b
将这些行添加到Project:Gradle
依赖关系{
classpath'com。 google.gms:google-services:3.0.0'
//注意:不要在这里放置应用程序依赖关系;它们属于单个模块build.gradle文件中的
// b
$ b 将这些添加到应用程序中:Gradle
依赖项{
compile'com.google.firebase:firebase-core:9.0。 0'
compile'com.google.firebase:firebase-database:9.0.0'
}
//将此添加到您的应用程序底层
apply plugin:'com.google.gms.google-services'
注意:请覆盖数据库中的规则。如果您没有执行登录身份验证。
{
rules:
{
.read:true,
.write:true,
}
}
$ b
在Application或Activity的onCreate中添加此行
Firebase.setAndroidContext(本);
Anyone looking at this the below is the correct answer and I had everything setup correctly.
I still dont no what the issue is.
I was loggin in with facebook, using that to create a firebaseuser object.
and then sending the below as test data.
I've found it to be an intermittent issue. Uninstalling the app from the device and redeploying often fixes the issue.
very odd
Im actually struggling to get the example to work.
So basic.. set value....
FirebaseDatabase database = FirebaseDatabase.getInstance();
Log.d(TAG, "db ref: "+database.getReference());
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
When I go into the database console I cant see the values I sent.
My log is returning a reference to the db though.
I've also set my rules up as public to be on the safe side.
Any ideas what I'm doing wrong?
解决方案
Actually you are referencing a child 'message' which is logically not there,
If you want to name you root of the database simply follow the steps
DatabaseReference myRef1 = FirebaseDatabase.getInstance().getReference(); //Getting root reference
myRef1.setValue("Hello, World!");
and if you want to give some value to your child in database
, Make sure you have a child in your database
DatabaseReference myRef1 = FirebaseDatabase.getInstance().getReference(); //Getting root reference
DatabaseReference myRef = myRef1.child("message"); //Write your child reference if any
myRef.setValue("Hello, World!");
It will overwrite
the value of your child to 'Hello World'
Go to Firebase console
and create a project and give it a valid packageName which you will use. and download google-service.json file and add this file to app folder
by changing your Android View to Project View.
Add these lines into Project:Gradle
dependencies {
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Add these into App:Gradle
dependencies {
compile 'com.google.firebase:firebase-core:9.0.0'
compile 'com.google.firebase:firebase-database:9.0.0'
}
// ADD THIS AT THE BOTTOM OF YOUR APP GRADLE
apply plugin: 'com.google.gms.google-services'
Note : Do overwrite the rules in your database . If you are not implementing login authentication.
{
"rules":
{
".read": true,
".write": true,
}
}
And add this line in Application or in onCreate of Activity
Firebase.setAndroidContext(this);
这篇关于基本的Firebase数据库保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!