Android App未连接到真实设备中的Firebase实时数据库

时间:2018-09-09 11:17:01

标签: android firebase firebase-realtime-database

我问过一个问题,我的应用程序仅在模拟器上运行,而不在真实设备上运行,这是它的链接:

Firebase database is not updating with no errors, slow response

不幸的是,我无法找出问题或解决问题。因此,我从头开始,构建了一个简单的应用程序,该应用程序将“ TEST”写入实时数据库,并且存在相同的问题,该应用程序只能在模拟器上运行,而不能在我的真实设备上运行。 因此,我发现当我在设备中运行该应用程序时,该应用程序无法连接到Firebase。但是我不知道为什么吗?

这是我的主要活动:

package com.example.alice.testingfirebase;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

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


public class MainActivity extends AppCompatActivity {


    private DatabaseReference mDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDatabase = FirebaseDatabase.getInstance().getReference();
        for (int i = 0; i<10;i++) {
            String id = i+"";
            mDatabase.child("users").child(id).setValue("TEST");
        }
    }
}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alice.testingfirebase">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.alice.testingfirebase"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}
apply plugin: 'com.google.gms.google-services'

数据库的规则设置为公共。

添加INTERNET权限后,该应用将继续停止,这是日志目录的一部分:

09-09 14:39:55.374 21502-21537/com.example.alice.testingfirebase V/FA: Connection attempt already in progress
09-09 14:39:55.455 21502-21502/com.example.alice.testingfirebase D/AndroidRuntime: Shutting down VM
09-09 14:39:55.456 21502-21502/com.example.alice.testingfirebase E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.alice.testingfirebase, PID: 21502
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alice.testingfirebase/com.example.alice.testingfirebase.MainActivity}: com.google.firebase.database.DatabaseException: Calls to setLogLevel() must be made before any other usage of FirebaseDatabase instance.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: com.google.firebase.database.DatabaseException: Calls to setLogLevel() must be made before any other usage of FirebaseDatabase instance.
        at com.google.firebase.database.FirebaseDatabase.zzb(Unknown Source:38)
        at com.google.firebase.database.FirebaseDatabase.setLogLevel(Unknown Source:3)
        at com.example.alice.testingfirebase.MainActivity.onCreate(MainActivity.java:22)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6938) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
09-09 14:39:55.472 21502-21551/com.example.alice.testingfirebase W/zygote64: Skipping duplicate class check due to unrecognized classloader
09-09 14:39:55.474 21502-21551/com.example.alice.testingfirebase I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
09-09 14:39:55.475 21502-21551/com.example.alice.testingfirebase I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
09-09 14:39:55.506 21502-21551/com.example.alice.testingfirebase W/zygote64: Skipping duplicate class check due to unrecognized classloader
09-09 14:39:55.567 21502-21559/com.example.alice.testingfirebase D/NetworkSecurityConfig: No Network Security Config specified, using platform default
09-09 14:39:55.573 21502-21559/com.example.alice.testingfirebase D/TcpOptimizer: TcpOptimizer-ON
09-09 14:40:00.281 21502-21521/com.example.alice.testingfirebase I/zygote64: WaitForGcToComplete blocked for 22.520ms for cause HeapTrim
09-09 14:40:00.286 21502-21521/com.example.alice.testingfirebase W/zygote64: Suspending all threads took: 5.439ms

3 个答案:

答案 0 :(得分:1)

请将其添加到清单文件中。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.alice.testingfirebase">
        <uses-permission android:name="android.permission.INTERNET"/>
        <application
            android:allowBackup="true"..
            ....
        </application>
</manifest>

也可以像这样编辑MainActivity

FirebaseDatabase obj= FirebaseDatabase.getInstance();
DatabaseReference mDatabase= obj.getReference();
for (int i = 0; i<10;i++) 
      {
         String id = i+"";
         mDatabase.child("users").child(id).setValue("TEST");
      }

答案 1 :(得分:1)

首先,您的AndroidManifext.xml没有Internet权限,我仍然想知道它如何在模拟器上工作。

第二,先初始化FirebaseDatabase,然后初始化DatabaseReference以能够在Firebase Realtime数据库中读写数据:

// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference().child("users").child("id"); //location to put, can be anything you want ...

myRef.setValue("TEST");

此外,您确定这就是您想要的吗?将字符串放在id字段中?考虑在控制台中检查您的FireBase数据库!

阅读: https://firebase.google.com/docs/database/android/start

答案 2 :(得分:0)

对于使用VIVO和OPPO手机的开发人员,这些品牌在连接Firebase时遇到一个奇怪的问题。我有7个不同品牌和配置的设备用于测试目的,除这两个设备外,其他所有设备都可以连接。没有错误,也没有例外。他们只是拒绝连接。只是想指出这一点,以便使用这些手机的开发人员知道该问题。