繁体   English   中英

无法写入我的 firebase 实时数据库

[英]Cannot Write in my firebase realtime database

我似乎无法理解为什么我不能写入我的数据库。 在我看来,有一些我无法解决的连接问题,因为我确实遵循了几个关于这个问题的教程,但都没有成功。

这是我检查过几次的几个关键点,但我想我还遗漏了其他东西......

这是我的 Firebase 实时数据库,只有一个孩子:

 {
   "message" : "nil"
 }

这是我的 Firebase 实时数据库规则:

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

这是我的项目 Gradle 依赖项:

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

这是我的模块 Gradle 依赖项:

...
   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'
...

我在 Android.Manifest.xml 中添加了 INTERNET 权限

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

我的实际代码来自 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");
  ...
  }
 }

我希望每当我启动应用程序时,因为我已经将 setValue 放在 onCreate() 方法中,所以我尊重的数据库/子项(例如“nil”)中的值将被“我记录”覆盖。 一些指示或想法将不胜感激,因为我已经坚持了几个星期,现在一遍又一遍地发布帖子/教程/关键点。 谢谢!

编辑 #1 这是我的 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 

尝试这样做:

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");
  ...
  }
 }

Codelab 示例: https : //codelabs.developers.google.com/codelabs/firebase-android/#1

我可能知道发生了什么。 我也花了很多时间在互联网上试图弄清楚这一点。 我的问题是,因为我的实时数据库服务器不在默认位置(我认为是 us-central1),我不得不将我的数据库引用复制到.getInstance()方法中。

例如,

如果您引用 Url 是

https://MyApp.firebase.app/

那么你的代码应该看起来像

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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