Rbar
Rbar

Reputation: 3928

write object with url to firebase database

I am trying to write an object, which includes a URL as one of its attributes, to my firebase database. It is failing with the error below.

Error

This error is longer (really long) but it just repeats the following and then ends here

at com.google.android.gms.internal.zzeas.zzbu(Unknown Source) at com.google.android.gms.internal.zzear.zzbq(Unknown Source) at com.google.android.gms.internal.zzear.zzbt(Unknown Source) at com.google.android.gms.internal.zzeas.zzbu(Unknown Source) at com.google.android.gms.internal.zzear.zzbq(Unknown Source) at com.google.android.gms.internal.zzear.zzbt(Unknown Source) at com.google.android.gms.internal.zzeas.zzbu(Unknown Source) at com.google.android.gms.internal.zzear.zzbq(Unknown Source) at com.google.android.gms.internal.zzear.zzbq(Unknown Source) at com.google.android.gms.internal.zzear.zzbp(Unknown Source) at com.google.firebase.database.DatabaseReference.zza(Unknown Source) at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) at com.companyName.appName.MyActivity.postDataToFirebase(MyActivity.java:582) at com.companyName.appName.MyActivity$13.onSuccess(MyActivity.java:551) at com.companyName.appName.MyActivity$13.onSuccess(MyActivity.java:543) at com.google.firebase.storage.zzj.zzi(Unknown Source) at com.google.firebase.storage.zzaa.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

I was able to track down that it crashes when I include my URL in the object I am writing to firebase, but it does not crash when I omit the URL.

Post Function in My Activity

public void postDataToFirebase() {
    Log.d(TAG, "triggering postDataToFirebase...");
    Uri myURL = https://somewebaddress.com/subpath%2Fotherpath%2FanotherPath%2FV3ipNtaGW3tG%2FhairColor%2F-Op032Dasdfie41ZB98d%2FeyeColor%2F3%2FasdfEesWEWf1341DfWds342%2FintroductionVideo.mp4?alt=media&token=a457i222l-6c6a2-2524l4-zy3m-p7384eo295d0di1;
    String greetingValue = "Hi there!";
    String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    Log.e(TAG, "This is the current time = " + timestamp);
    // Create HashMap of all details to post
    Map<String, Object> childUpdate = new HashMap<>();
    childUpdate.put("firstName", firstNameTextField.getText().toString());
    childUpdate.put("lastName", lastNameTextField.getText().toString());
    childUpdate.put("hairColor", hairColorTextField.getText().toString());
    childUpdate.put("greetingValue", greetingValue);
    childUpdate.put("timestamp", timestamp);
    childUpdate.put("URL", myURL);  // **NOTE: If I comment out this line, no error occurs 
    Log.d(TAG, "onSuccess: childUpdate");
    Log.d(TAG, childUpdate.toString());
    // **ISSUE: The error occurs on the line below
    myRef.child("somepath")child(userId).child("someotherpath").setValue(childUpdate);
}

Build.Gradle (app)

I am including this because it was asked for in the question referenced below

apply plugin: 'com.android.application'

repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile "com.android.support:support-v4:25.3.1"
    compile "com.android.support:support-v13:25.3.1"
    compile "com.android.support:cardview-v7:25.3.1"
    compile "com.android.support:appcompat-v7:25.3.1"
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-auth:11.4.2'
    compile 'com.google.firebase:firebase-database:11.4.2'
    compile 'com.google.firebase:firebase-crash:11.4.2'
    compile 'com.google.firebase:firebase-config:11.4.2'
    compile 'com.google.firebase:firebase-storage:11.4.2'
    compile 'com.google.firebase:firebase-messaging:11.4.2'
    compile 'com.google.firebase:firebase-core:11.4.2'
    compile 'com.firebase:firebase-jobdispatcher:0.6.0'
    compile 'com.android.volley:volley:1.0.0'
    testCompile 'junit:junit:4.12'
}

List<String> dirs = [
        'main',     // main sample code; look here for the interesting stuff.
        'common',   // components that are reused by multiple samples
        'template'] // boilerplate code that is generated by the sample template process

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "com.company.appname"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 19
        versionName "4.8"
        multiDexEnabled true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            dirs.each { dir ->
                java.srcDirs "src/${dir}/java"
                res.srcDirs "src/${dir}/res"
            }
        }
        androidTest.setRoot('tests')
        androidTest.java.srcDirs = ['tests/src']

    }


    buildTypes {
        release {
            minifyEnabled true // Enables code shrinking for the release build type.
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
    productFlavors {
    }

}

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

Additional:

I also found this question which appeared to be similar based on the error, but I checked and I am able to make updates (which was not possible in this question), making me think this is not the same issue.

Does anyone understand why my post to firebase is succeeding when I don't include the url but failing when I do (and how to prevent it)?

Upvotes: 2

Views: 982

Answers (3)

Joseph Varghese
Joseph Varghese

Reputation: 609

Convert it into a string and then upload into firebase database

Upvotes: 0

Bob Snyder
Bob Snyder

Reputation: 38299

Uri is not one of the native types supported by Firebase Database for storing. You need to store the string equivalent instead: Uri.toString().

Later when you read the string and want to recreate the Uri, you can use Uri.parse().

Upvotes: 5

anashamidkh
anashamidkh

Reputation: 39

myURL type is URI & firebase does not allow to store URI. Firebase allows only String, Long, Double, Boolean, Map, List. (see https://firebase.google.com/docs/database/android/read-and-write).

If you want to store your URL, store it in String.

String myURL = "https://somewebaddress.com/subpath%2Fotherpath%2FanotherPath%2FV3ipNtaGW3tG%2FhairColor%2F-Op032Dasdfie41ZB98d%2FeyeColor%2F3%2FasdfEesWEWf1341DfWds342%2FintroductionVideo.mp4?alt=media&token=a457i222l-6c6a2-2524l4-zy3m-p7384eo295d0di1;"

Upvotes: 1

Related Questions