Android Question json parsing firebase

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
Hi, I'm working with firebase real time, when I insert a record in the base, it returns me what I insert into childadded

DataSnapshot {key = -Kb7Usx5jqf2LM_-zYLy, value = {Uid = CLSgTB9noBQVh5fheGqfvEmegWu1, tstamp2 = 01/22/2017, 20:25:18, UNam = Gabriela Riccomini, Title = null, tstamp = 1485127518112, neighborhood = Jularo, Body = test }})

The question is: could you give me the json code that allows me to assign values to where I want?

I do not understand how the json.parser works
regards
 

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
I do not know what I'm wrong with the truth, I get this error
B4X:
Sub Reference_onChildAdded(snapshot As Object, child As String)
    Log($"Reference_onChildAdded(${snapshot})"$)
    Log(child)
  
    Log($"ref_onDatachange()"$)
    Dim snap As DataSnapshot = snapshot
    Log("Value="&snap.Value)

    Dim parser As JSONParser
    parser.Initialize(snapshot)
    Dim root As Map = parser.NextValue
    Dim json As String = root.Get("title")
  

  
  
End Sub


error:
Value={Uid=CLSgTB9noBQVh5fheGqfvEmegWu1, tstamp2=01/22/2017, 20:25:18, UNam=Gabriela Riccomini, Title=null, tstamp=1485127518112, barrio=Jularo, Body=(EditText): Left=0, Top=748, Width=498, Height=124, Tag=, Text=}

Error occurred on line: 531 (Main) is Dim json As String = root.Get("title")

java.lang.ClassCastException: java.lang.String cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap

at anywheresoftware.b4a.objects.collections.Map.Get(Map.java:63)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)

at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)

at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)

at anywheresoftware.b4a.BA$2.run(BA.java:328)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:135)

at android.app.ActivityThread.main(ActivityThread.java:5593)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
ub Reference_onDataChange(snapshot As Object)
    Log($"ref_onDatachange()"$)
    Dim snap As DataSnapshot = snapshot
    Log($"${GetType(snap.Value)}"$)
    Log($"${snap.Key}"$)
    Log($"${snap.Value}"$)
    If snap.Value Is Map Then
        Dim m As Map = snap.Value
        If m.IsInitialized Then
            t.Enabled = True
        End If
        If m.ContainsKey("posts") Then
            Log($"m.ContainsKey("posts")"$)
            Log($"${m.Get("posts")}"$)
        End If   
        Log("Map "&n)

    End If
End Sub
 
Upvote 0

gregorio_adrian_gimenez

Active Member
Licensed User
Longtime User
Thanks Jeffrey!, Thank you very much DonManfred!! Your code worked perfect!

I ask you a question, if I generate a chat, should I save the map on my cell phone? Or every time a user loads a comment, I download all the comments in all the members? How wassap did
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
He means like whatsapp sure.
But he want to try the Firebase Realtimedatabase to do the Datatransport.

Honestly; i dont think it will work fine with this version of the lib. It is incomplete, as of now not working perfect.
I dont suggest to use the lib.

Instead using a local db and firebase messaging will be a better way to build chat systems i guess
 
Upvote 0
Top