Hannes
Hannes

Reputation: 311

Firebase Parsing Error

Whats wrong with my model?

I´m using the Android RecyclerViewAdapter. Its a pretty basic setup - mostly copied from the android examples on GitHub.

Error im getting: (Updated to complete Log)

07-08 18:37:49.324 24650-24650/com.brueller E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.crash.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/com.brueller-2/base.apk"],nativeLibraryDirectories=[/data/app/com.brueller-2/lib/arm64, /vendor/lib64, /system/lib64]]
07-08 18:37:50.392 24650-24650/com.brueller E/MainActivity: LAT: 51.5879459| LONG: 10.2771444
07-08 18:37:52.097 24650-24650/com.brueller E/UncaughtException: com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.brueller.model.PostModel
                                                                     at com.google.android.gms.internal.zzaix.zzd(Unknown Source)
                                                                     at com.google.android.gms.internal.zzaix.zzb(Unknown Source)
                                                                     at com.google.android.gms.internal.zzaix.zza(Unknown Source)
                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
                                                                     at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
                                                                     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
                                                                     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
                                                                     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
                                                                     at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1482)
                                                                     at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
                                                                     at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4037)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:886)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:698)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:630)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
                                                                     at android.os.Handler.handleCallback(Handler.java:743)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:150)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5639)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

My Model Class:

public class PostModel {

public String getPostContent() {
    return mPostContent;
}

public void setPostContent(String mPostContent) {
    this.mPostContent = mPostContent;
}

public Long getTimeStamp() {
    return mTimeStamp;
}

public void setTimeStamp(Long mTimeStamp) {
    this.mTimeStamp = mTimeStamp;
}

public String getPostalCode() {
    return mPostalCode;
}

public void setPostalCode(String mPostalCode) {
    this.mPostalCode = mPostalCode;
}

private String mPostContent;
private Long mTimeStamp;
private String mPostalCode;

}

Firebase Data exported to json via website:

{
  "test" : {
    "postContent" : "hallo",
    "postalCode" : "054587",
    "timeStamp" : 1467915869142
  }
}

ViewHolder:

public class PostHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.PostContent) TextView postContentView;
    @BindView(R.id.TimeString) TextView timeStringView;

    View mView;

    public PostHolder (View itemView) {
        super(itemView);
        mView = itemView;

        ButterKnife.bind(this, mView);
    }

    public void applyModel(PostModel _postModel) {
        postContentView.setText(_postModel.getPostContent());

        // Convert long to nice string
        timeStringView.setText(
            android.text.format.DateUtils.getRelativeTimeSpanString((Long)
                    _postModel.getTimeStamp()).toString());
    }
}

Upvotes: 0

Views: 2122

Answers (2)

Hannes
Hannes

Reputation: 311

thanks for the answers. I just solved it.

Incase it helps somebody:

The problem was that i wrote the data in the wrong way.

What i did: mRef.child("someChild").setValue(myClass); which results to writing the model-data directly under the "someChild" node.

What i needed: mRef.child("someChild").push().setValue(myClass); This instructs FireBase to create a new unique id as topnode which holds the modeldata.

I complety overlooked it multiple times :(.

Thanks

Upvotes: 1

Rashed Doha
Rashed Doha

Reputation: 53

You should post your error log so we can get a better idea of what's going wrong. But Firebase uses the Jackson ObjectMapper to convert objects to Json and it requires you to have an empty constructor in your model class.

Upvotes: 1

Related Questions