Basic Firebase database save

后端 未结 2 938
庸人自扰
庸人自扰 2021-01-15 13:20

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

相关标签:
2条回答
  • 2021-01-15 14:04

    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);
    
    0 讨论(0)
  • 2021-01-15 14:20

    seems, i found the answer. It helps me!

    I have used before android studio version 1.5 And that's why (maybe not) but i could't upgrade required firebase libs to 9.0.2 All this time i have used 9.0.0

    So firstly i upgrade my android studio to updated stable version 2.2 and then update version of dependencies to next ones:

    compile 'com.google.firebase:firebase-core:9.0.2'
    compile 'com.google.firebase:firebase-auth:9.0.2'
    compile 'com.google.firebase:firebase-database:9.0.2'
    

    And now all values are saved to database from android app.

    So do two things: 1. update android studio to last stable version 2. update firebase required libs versions

    Hope this answer will help

    0 讨论(0)
提交回复
热议问题