Android Question Strange JSONParser behaviour

Descartex

Well-Known Member
Licensed User
Longtime User
Hi.
I'm working with JSON strings that a webservice sends to my app, but it crashes with a strange behaviour.
If I do this:
B4X:
Log(JSONParser.NextArray.Size)
                Dim l As List
                l=JSONParser.NextArray
On the log it puts this:
2
Error occurred on line: 156 (Main)
org.json.JSONException: End of input at character 453 of [{"j1":"976","n1":"10","j2":"974","n2":"4","j3":"971","n3":"7","j4":"973","n4":"9","j5":"978","n5":"28","j6":"972","n6":"44","j7":"975","n7":"55","j8":"980","n8":"57","j9":"979","n9":"74","j10":"977","n10":"31","equipo":"17"},{"j1":"1000","n1":"1","j2":"998","n2":"4","j3":"996","n3":"5","j4":"997","n4":"9","j5":"993","n5":"15","j6":"995","n6":"57","j7":"999","n7":"77","j8":"994","n8":"78","j9":"1001","n9":"88","j10":"992","n10":"10","equipo":"20"}]
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:60)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$2.run(BA.java:338)
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:5665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
It means that the list returned by NextArray has 2 elements, but it crashes when I try to assign to a list to handle it.
I've tried the online tool to check the string is right and it seems yes...
parser.png

Can someone bring some light on this?
Thank you in advance.
 

rscheel

Well-Known Member
Licensed User
Longtime User
Probaste algo como esto

B4X:
Sub ObtieneTiposEventos
    Dim j As HttpJob
    j.Initialize("", Me) 'name is empty as it is no longer needed
    j.download2(ModConn.Url_php_mysql("TPEVENTOS"), Array As String ("REQUEST_METHOD", "GET_TPEVENTOS", "USER_ID", ModConn.IMEI.GetDeviceId))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'The result is a json string. We parse it and log the fields.
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Dim quotes As List = jp.NextArray
        For Each quot As Map In quotes
            Log(quot.Get("j1"))
        Next
    End If
    'j.Release

End Sub
 
Upvote 0

Descartex

Well-Known Member
Licensed User
Longtime User
It's strange, if I access the items just after initialize the parser it works, but if i make some access to the parser, for example, to its size, it crashes.
It can be possible it "unloads" it after the first access?

Thanks anyway, @rscheel .
 
Upvote 0
Top