Android Question Error with jRoot.get call

Hi!
I am calling a web service with JSON result. And during JSON parsing my app is behaving strange. Sometimes the call is successuful sometime there is an error:
(ClassCastException) java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
Here is the code and the error is in line 38. It is al little confusing that the error is happening only sometimes. When I add some line, for example log after line 69 then the error is missing. But this appens only in debug mode. In release (obfuscated) mode the error is permanent.

B4X:
Public Sub CallServisCistoce As ResumableSub
    Dim Result As String
    Dim URL As String = "https://moja.cistoca-vz.hr/Public/JSONgetRasporedPoKoordinatamaAdresiDrugiPut"
    Dim m As Map = CreateMap("IdUlice": "1641", "kbrCijeli":  "6", "idSkupine" : "1", "idSKBR" : "65550")
    Dim Parameters As String = m.As(JSON).ToString

    Log("sistemsko:" & DateTime.Date(DateTime.Now))
    
    Dim format As String = "dd.MM.yyyy"
    SetDateFormat("hr", "HR", format)
    Dim sada As Long
    Dim sada_str As String
    sada = DateTime.Now
    sada_str = DateTime.Date(DateTime.Now)

    Dim j As HttpJob
    Try   
        j.Initialize("", Me)
        j.PostString(URL, Parameters)
        j.GetRequest.SetContentType("application/json")

        Wait For (j) JobDone(j As HttpJob)       

        If j.Success Then           

            Result = j.GetString       
            Log("Result:" & Result)   
            
            Dim parser As JSONParser                           
            
            parser.Initialize(Result)

            Dim jRoot As Map = parser.NextObject                           

            Dim ListaOtpada As List
            ListaOtpada.Initialize

            ListaOtpada = jRoot.Get("dtRaspored")

            For Each m As Map In ListaOtpada.As(JSON).ToList
                
                DateTime.DateFormat = "yyyy-MM-dd"
                Dim t As Long
                Dim datum1 As String               
                datum1 = m.Get("datum")
                datum1 = datum1.SubString2(0,10)
                t = DateTime.DateParse(datum1)
                'Log("t:" &t)
                DateTime.DateFormat = "dd.MM.yyyy"

                Dim s As String
                s = DateTime.Date(t)               
                m.Put("startTime",t)
                If t >= sada Then                                   
                    'Log("redak" & m.Get("datum") & " " & m.Get("nazivTipa") & "startTime:" & m.Get("startTime"))
                    'Log("s:" & s)
                    
                    Dim otp As Otpad
                    otp.datum = s
                    otp.datumInt = m.Get("startTime")
                    otp.spremnik = m.Get("nazivTipa")
                    otp.tip = m.Get("subject")
                    otp.boja = m.Get("boja")
                    otpadLista.Add(otp)
                End If                               
                                
            Next   
                    
            Log ("otpadLista size:" & otpadLista.Size)
            otpadLista.SortType("datumInt", True)   
                        
        Else
            Result = j.ErrorMessage
        End If
    Catch
        Log(LastException)
    End Try
    
    
    
    j.Release

    Log(Parameters)
    Log(Result)
End Sub
 
Result:{"Success":true,"dtRaspored":"[{\"datum\":\"2025-01-06T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-06T12:00:00\",\"endTime\":\"2025-01-06T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Crni spremnik\",\"boja\":\"#000000\",\"subject\":\"Miješani komunalni otpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - miješani komunalni otpad\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Plavi spremnik\",\"boja\":\"#0000FF\",\"subject\":\"Papir - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - papir\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-01-20T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-20T12:00:00\",\"endTime\":\"2025-01-20T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-01-27T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-27T12:00:00\",\"endTime\":\"2025-01-27T12:00:00\",\"nazivTipa\":\"Crni spremnik\",\"boja\":\"#000000\",\"subject\":\"Miješani komunalni otpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - miješani komunalni otpad\"},{\"datum\":\"2025-01-27T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-27T12:00:00\",\"endTime\":\"2025-01-27T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-01-27T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-27T12:00:00\",\"endTime\":\"2025-01-27T12:00:00\",\"nazivTipa\":\"Žuti spremnik\",\"boja\":\"#fcb314\",\"subject\":\"Plastika - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - plastika\"},{\"datum\":\"2025-02-03T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-02-03T12:00:00\",\"endTime\":\"2025-02-03T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-02-04T00:00:00\",\"danUTjednu\":\"Tuesday\",\"startTime\":\"2025-02-04T12:00:00\",\"endTime\":\"2025-02-04T12:00:00\",\"nazivTipa\":\"Sivi spremnik\",\"boja\":\"#999999\",\"subject\":\"Metal - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - metal\"},{\"datum\":\"2025-02-10T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-02-10T12:00:00\",\"endTime\":\"2025-02-10T12:00:00\",\"nazivTipa\":\"Crni spremnik\",\"boja\":\"#000000\",\"subject\":\"Miješani komunalni otpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - miješani komunalni otpad\"},{\"datum\":\"2025-02-10T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-02-10T12:00:00\",\"endTime\":\"2025-02-10T12:00:00\",\"nazivTipa\":\"Plavi spremnik\",\"boja\":\"#0000FF\",\"subject\":\"Papir - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - papir\"},{\"datum\":\"2025-02-10T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-02-10T12:00:00\",\"endTime\":\"2025-02-10T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-02-17T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-02-17T12:00:00\",\"endTime\":\"2025-02-17T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A -...
Message longer than Log limit (4000). Message was truncated.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi!
I am calling a web service with JSON result. And during JSON parsing my app is behaving strange. Sometimes the call is successuful sometime there is an error:
(ClassCastException) java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
Here is the code and the error is in line 38. It is al little confusing that the error is happening only sometimes. When I add some line, for example log after line 69 then the error is missing. But this appens only in debug mode. In release (obfuscated) mode the error is permanent.
Using the tool you will get the code which parsing the json.
jsonEX.png
 
Upvote 0
This works OK, but I have to parse dtRaspored.For example, this is shortened JSON:
{"Success":true,"dtRaspored":"[{\"datum\":\"2025-01-06T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-06T12:00:00\",\"endTime\":\"2025-01-06T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Crni spremnik\",\"boja\":\"#000000\",\"subject\":\"Miješani komunalni otpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - miješani komunalni otpad\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Plavi spremnik\",\"boja\":\"#0000FF\",\"subject\":\"Papir - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - papir\"},{\"datum\":\"2025-01-13T00:00:00\",\"danUTjednu\":\"Monday\",\"startTime\":\"2025-01-13T12:00:00\",\"endTime\":\"2025-01-13T12:00:00\",\"nazivTipa\":\"Smeđi spremnik\",\"boja\":\"#663300\",\"subject\":\"Biootpad - 06:00\",\"description\":\"GORNJI KNEGINEC - Skupina A - biootpad\"}]","dtAdresaUsluge":"[{\"idAdrese\":48801,\"adresa\":\"Ulica učitelja Vjekoslava Kezele 6, Gornji Kneginec ,42204 Turčin\",\"idUlice\":1641,\"idNaselja\":40,\"kbrCijeli\":\"6\",\"idAdrese1\":48801,\"idSkbr\":65550}]","resultCombo":[{"idSkupine":1,"nazivSkupine":"A pojedinačni spremnici"}],"isDefaultSkupina":1,"brojDanaZaPrikaz":"336","odabraniDatum":"\/Date(1738162537862)\/"}
 
Upvote 0

teddybear

Well-Known Member
Licensed User
This works OK, but I have to parse dtRaspored.For example, this is shortened JSON:
1.png

Continue parsing the JSON string as the code told you, you will get dtRaspored.
Repeat the step until you get the result you want
2.png
 
Upvote 0
Top