简体   繁体   中英

Cannot Write in my firebase realtime database

I cannot seem to get my head around why I cannot Write into my database. It looks to me that there is some connection issue I cannot resolve, because I did follow several tutorials on this matter and none of them resulted with success.

Here are several keypoints I've checked several times, but I guess I am missing something else...

Here is my Firebase realtime database with only single child:

 {
   "message" : "nil"
 }

Here is my Firebase realtime database Rules:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Here is my project Gradle dependencies:

...
   dependencies {
       classpath 'com.android.tools.build:gradle:3.4.2'
       classpath 'com.google.gms:google-services:4.2.0'
   }
...

Here is my module Gradle dependencies:

...
   dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'androidx.appcompat:appcompat:1.0.2'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      implementation 'com.google.firebase:firebase-core:17.0.0'
      implementation 'com.google.firebase:firebase-database:18.0.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test:runner:1.2.0'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     implementation 'com.google.android.material:material:1.0.0'
   }
  apply plugin: 'com.google.gms.google-services'
...

I have added INTERNET permissions in my Android.Manifest.xml

...
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>
... 

And my actual code from MainActivity.java

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   public DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference();
   public DatabaseReference cinemasRef = myRootRef.child("message");
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       Toolbar toolbar;
       ActionBarDrawerToggle toggle;
       cinemasRef.setValue("I logged");
  ...
  }
 }

I expect that whenever I start the application, since I've put setValue in onCreate() method, that the value in my respected database/child (eg "nil") would be overridden with "I logged". Some pointers or ideas would be greatly appreciated since I've stuck on this for few weeks now going over and over again posts/tutorials/keypoints. Thanks!

EDIT #1 Here is my Logcat

    2019-08-09 16:48:30.840 9474-9474/? D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2019-08-09 16:48:30.847 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2019-08-09 16:48:30.849 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2019-08-09 16:48:30.851 9474-9474/? D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2019-08-09 16:53:12.302 9474-9474/? I/example.scratc: Not late-enabling -Xcheck:jni (already on)
2019-08-09 16:53:12.336 9474-9474/? E/example.scratc: Unknown bits set in runtime_flags: 0x8000
2019-08-09 16:53:12.337 9474-9474/? W/example.scratc: Unexpected CPU variant for X86 using defaults: x86
2019-08-09 16:53:13.019 9474-9474/com.example.scratch W/example.scratc: JIT profile information will not be recorded: profile file does not exits.
2019-08-09 16:53:13.022 9474-9474/com.example.scratch I/chatty: uid=10135(com.example.scratch) identical 10 lines
2019-08-09 16:53:13.022 9474-9474/com.example.scratch W/example.scratc: JIT profile information will not be recorded: profile file does not exits.
2019-08-09 16:53:13.435 9474-9474/com.example.scratch V/FA: Registered activity lifecycle callback
2019-08-09 16:53:13.495 9474-9474/com.example.scratch I/FirebaseInitProvider: FirebaseApp initialization successful
2019-08-09 16:53:13.499 9474-9474/com.example.scratch I/InstantRun: starting instant run server: is main process
2019-08-09 16:53:13.691 9474-9474/com.example.scratch D/DEBUG: THIS IS ONCREATE ????????????????????????????????????
2019-08-09 16:53:13.693 9474-10282/com.example.scratch V/FA: Collection enabled
2019-08-09 16:53:13.693 9474-10282/com.example.scratch V/FA: App package, google app id: com.example.scratch, 1:801495270237:android:57be8c2d1041efb4
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: App measurement is starting up, version: 16250
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2019-08-09 16:53:13.701 9474-10282/com.example.scratch I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.scratch
2019-08-09 16:53:13.702 9474-10282/com.example.scratch D/FA: Debug-level message logging enabled
2019-08-09 16:53:13.731 9474-9474/com.example.scratch V/FA: onActivityCreated
2019-08-09 16:53:13.776 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2019-08-09 16:53:13.777 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2019-08-09 16:53:13.784 9474-10289/com.example.scratch D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-08-09 16:53:13.925 9474-10282/com.example.scratch V/FA: Connecting to remote service
2019-08-09 16:53:13.993 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->doBeforeTextChanged()V (greylist-max-p, reflection, denied)
2019-08-09 16:53:13.993 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->doAfterTextChanged()V (greylist-max-p, reflection, denied)
2019-08-09 16:53:13.994 9474-9474/com.example.scratch W/example.scratc: Accessing hidden method Landroid/widget/AutoCompleteTextView;->ensureImeVisible(Z)V (greylist-max-p, reflection, denied)
2019-08-09 16:53:14.006 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.211 9474-10282/com.example.scratch I/FA: Tag Manager is not found and thus will not be used
2019-08-09 16:53:14.219 9474-10282/com.example.scratch D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=5326573072556222733}]
2019-08-09 16:53:14.271 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.271 9474-10282/com.example.scratch V/FA: Connection attempt already in progress
2019-08-09 16:53:14.277 9474-10282/com.example.scratch V/FA: Activity resumed, time: 50397964
2019-08-09 16:53:14.334 9474-10286/com.example.scratch D/OpenGLRenderer: Swap behavior 1
2019-08-09 16:53:14.336 9474-10286/com.example.scratch W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-08-09 16:53:14.336 9474-10286/com.example.scratch D/OpenGLRenderer: Swap behavior 0
2019-08-09 16:53:14.347 9474-10286/com.example.scratch D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2019-08-09 16:53:14.347 9474-10286/com.example.scratch D/EGL_emulation: eglCreateContext: 0xe39e4880: maj 3 min 0 rcv 3
2019-08-09 16:53:14.375 9474-10286/com.example.scratch D/EGL_emulation: eglMakeCurrent: 0xe39e4880: ver 3 0 (tinfo 0xd81e8170)
2019-08-09 16:53:14.525 9474-10286/com.example.scratch W/Gralloc3: mapper 3.x is not supported
2019-08-09 16:53:14.600 9474-10286/com.example.scratch D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0
2019-08-09 16:53:14.674 9474-10286/com.example.scratch D/EGL_emulation: eglMakeCurrent: 0xe39e4880: ver 3 0 (tinfo 0xd81e8170)
2019-08-09 16:53:14.683 9474-10286/com.example.scratch D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
2019-08-09 16:53:14.866 9474-9474/com.example.scratch D/DEBUG: THIS IS ONCREATEOPTIONMENU!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2019-08-09 16:53:14.875 9474-9474/com.example.scratch I/Choreographer: Skipped 38 frames!  The application may be doing too much work on its main thread.
2019-08-09 16:53:14.900 9474-10282/com.example.scratch D/FA: Connected to remote service
2019-08-09 16:53:14.902 9474-10282/com.example.scratch V/FA: Processing queued up service tasks: 4
2019-08-09 

Try doing this:

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

   public DatabaseReference myRootRef, cinemasRef;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   Toolbar toolbar;
   ActionBarDrawerToggle toggle;
   // Moving the initialisation in function
   myRootRef = FirebaseDatabase.getInstance().getReference();
   cinemasRef = myRootRef.child("message");
   cinemasRef.setValue("I logged");
  ...
  }
 }

A Codelab example: https://codelabs.developers.google.com/codelabs/firebase-android/#1

I may know what is happening. I also spent lots of time in the internet trying to figure this out. My problem was that because my Realtime DataBase server was not in the default location (us-central1 I think) i had to copy my database reference an paste inside the .getInstance() method.

For example,

if you reference Url is

https://MyApp.firebase.app/

then your code should look like

FirebaseDatabase.getInstance(https://MyApp.firebase.app/).getReference("YourMessage").setValue("YourValue");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM