Android App is not connecting to firebase realtime database in a real device

Android App is not connecting to firebase realtime database in a real device



I've asked a question before where my app was working only on the emulator but not a real device, here's a link of it:



Firebase database is not updating with no errors, slow response



unfortunately, I wasn't able to figure out the issue or solve it. So, I started from scratch and build a simple app that writes "TEST" to a real-time database, and the same issue was there, the app is working ONLY on the emulator but not my real device.
So, I figured out that the app is not able to connect to firebase when I ran it in my device. But I couldn't figure out why?



Here's my main activity:


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





Here's my manifest:


<?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>



and here's my 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'



The rules of the database are set to be public.



After adding the INTERNET permission, the app keeps stopping, and here's part of the log cat:


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






is your device connected to internet?

– Sushant Somani
Sep 9 '18 at 11:19






Yes, it is. And I tried with both WiFi and Network data

– Alice J
Sep 9 '18 at 11:22






I've edited my answer.Please try that.

– Sushant Somani
Sep 9 '18 at 11:51




2 Answers
2



Please add this to your manifest file.


<?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>



Also edit your MainActivity like


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






The app now keeps stopping

– Alice J
Sep 9 '18 at 11:36






can you share some logs when the app stops.

– Sushant Somani
Sep 9 '18 at 11:38






what is the android OS you are using?

– Sushant Somani
Sep 9 '18 at 11:39






I updated the question with logcat. I'm using android 8

– Alice J
Sep 9 '18 at 11:43






it works!! Thanks

– Alice J
Sep 9 '18 at 13:41



First, you don't have internet permission in your AndroidManifext.xml and I'm still wondering how it does works on the emulator.


AndroidManifext.xml



Second, initialize FirebaseDatabase then DatabaseReference to be able to read-write data in Firebase Realtime database:


FirebaseDatabase


DatabaseReference


// 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");



Also, are you sure this is what you want? Putting String in an id field? Consider checking your FireBase Database in console!



Read: https://firebase.google.com/docs/database/android/start



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)