Android Question Json reading difficulties

cirollo

Active Member
Licensed User
Longtime User
Hi!
I'm having some problems reading a JSON.

I've 2 ws, called GetUser and GetAlunni, this how I consume them:

GetUser
B4X:
....
    ' "consumiamo" il WS
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   'Send a GET request
   job1.Download("http://myip.it:3015/special/GetUser?USERWEB='"&TxtUser.Text.Trim&"'&PWDUWEB='"&TxtPassword.Text.Trim&"'")
....

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   Log(Job.ErrorMessage)
   If Job.Success = True Then
       Dim res As String
       res = Job.GetString
       Log("Back from Job:" & Job.JobName )
       Log("Response from server: " & res)
       ' facciamo il parsing del json ricevuto
       Dim parser As JSONParser
       parser.Initialize(res)
       Dim root As List = parser.NextArray
       For Each colroot As Map In root
           USERWEB = colroot.Get("USERWEB")
           PWDUWEB = colroot.Get("PWDUWEB")
           DBNAME = colroot.Get("DBNAME")      
           Log("USERWEB = "&USERWEB.Trim&" ("&TxtUser.Text.ToUpperCase&") - PWDUWEB = "&PWDUWEB.Trim&" ("&TxtPassword.Text.ToUpperCase&") - DBNAME = "&DBNAME.Trim)
       Next
       If USERWEB.trim = TxtUser.Text.trim.ToUpperCase And PWDUWEB.trim = TxtPassword.Text.trim.ToUpperCase Then
           ok_procedi = True
       End If
   Else
       Log("Error: " & Job.ErrorMessage)
       Msgbox(Job.ErrorMessage,"Errore")
       ok_procedi = False
   End If
   Job.Release
   ProgressDialogHide
   Log(ok_procedi)
   ' se ho dei dati procedo
   If ok_procedi = True Then
       Activity.Finish
       StartActivity(Studenti)
   End If

ws response:
[{"USERWEB":"10028 ","PWDUWEB":"WEBBAACI ","DBNAME":"FS_SCHOOL"}]

I'm able to read this Json!

GetAlunni
B4X:
....
    Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   job1.Download("http://myip.it:3015/special/GetAlunni?USERWEB='"&Main.USERWEB.Trim&"'&PWDUWEB='"&Main.PWDUWEB.Trim&"'&DBNAME='"&Main.DBNAME.Trim&"'")
....

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   Log(Job.ErrorMessage)
   If Job.Success = True Then
       Dim res As String
       res = Job.GetString
       Log("Back from Job:" & Job.JobName )
       Log("Response from server: " & res)
       ' facciamo il parsing del json ricevuto
       Dim parser As JSONParser
       parser.Initialize(res)
  
       If Job.JobName="Job1" Then
           Log("job1 - alunni")
           Dim root As List = parser.NextArray
           'svuoto la tabella
           SQL1.ExecNonQuery("DELETE FROM STUDENTI")
           For Each colroot As Map In root
               Dim CodStude As String = colroot.Get("CODSTUDE")
               Dim DESSCUOL As String = colroot.Get("DESSCUOL")
               Dim CLASTUDE As String = colroot.Get("CLASTUDE")
               Dim SEZSTUDE As String = colroot.Get("SEZSTUDE")
               Dim VALBUONI As Double = colroot.Get("VALBUONI")
               Dim DTNASSTU As String = colroot.Get("DTNASSTU")
               Dim COGSTUDE As String = colroot.Get("COGSTUDE")
               Dim NOMSTUDE As String = colroot.Get("NOMSTUDE")
               Dim CODSCUOL As String = colroot.Get("CODSCUOL")
               Dim DESDIETA As String = colroot.Get("DIETA")
               ' inseriamo i dati nella tabella
               SQL1.ExecNonQuery2("INSERT INTO Studenti VALUES (?,?,?,?,?,?,?,?,?,?)", Array As Object (CodStude.Trim, COGSTUDE.Trim, NOMSTUDE.Trim, CODSCUOL.Trim, DESSCUOL.Trim, CLASTUDE.Trim, SEZSTUDE.Trim, DTNASSTU.Trim, VALBUONI, DESDIETA.Trim))
           Next
       End If
      
   Else
       Log("Error: " & Job.ErrorMessage)
       '   ToastMessageShow("Error: " & Job.ErrorMessage, True)
       Msgbox(Job.ErrorMessage,"Errore")
       ok_procedi = False
   End If
   Job.Release
   ProgressDialogHide
End Sub

ws response:
[{"CODSTUDE":"0000010028","COGSTUDE":"BARBETTA","NOMSTUDE":"GABRIELE","DTNASSTU":"","CODSCUOL":"_NESSUNA","DESSCUOL":"NON FREQUENTANTE","CLASTUDE":"","SEZSTUDE":"","VALBUONI":".00","DESDIETA":"NORMALE"},{"CODSTUDE":"0000013231","COGSTUDE":"BARBETTA","NOMSTUDE":"ALESSANDRO","DTNASSTU":"","CODSCUOL":"GARIB. E","DESSCUOL":"GARIBALDI ELEMENTARE","CLASTUDE":"3","SEZSTUDE":"B","VALBUONI":"-35.00","DESDIETA":"NORMALE"}]

I get the following error:
B4X:
JobName = Job1, Success = true
Back from Job:Job1
Response from server: {}
job1 - alunni
Error occurred on line: 204 (Studenti)
java.lang.RuntimeException: JSON Array expected.
   at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
   at b4a.example.studenti._jobdone(studenti.java:672)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:733)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:355)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
   at anywheresoftware.b4a.BA$2.run(BA.java:365)
   at android.os.Handler.handleCallback(Handler.java:808)
   at android.os.Handler.dispatchMessage(Handler.java:101)
   at android.os.Looper.loop(Looper.java:166)
   at android.app.ActivityThread.main(ActivityThread.java:7425)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

the Json seems empty, why?

regards,
Ciro
 
Last edited:

BillMeyer

Well-Known Member
Licensed User
Longtime User
Use this online tool to check your JSON https://b4x.com:51041/json/index.html

I checked your second "ws response:" and right at the end you are missing a "]" - I put that in and was able to read your string.

So my conclusion is that the string you are getting from your server is incomplete.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
sorry, the "]" is present, was a Ctrl+C Ctrl+V error.....

the tool correctly parse the JSON and this is the resulting code ....not quite different from mine...

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim CODSTUDE As String = colroot.Get("CODSTUDE")
 Dim DESSCUOL As String = colroot.Get("DESSCUOL")
 Dim CLASTUDE As String = colroot.Get("CLASTUDE")
 Dim SEZSTUDE As String = colroot.Get("SEZSTUDE")
 Dim VALBUONI As String = colroot.Get("VALBUONI")
 Dim DTNASSTU As String = colroot.Get("DTNASSTU")
 Dim COGSTUDE As String = colroot.Get("COGSTUDE")
 Dim DESDIETA As String = colroot.Get("DESDIETA")
 Dim NOMSTUDE As String = colroot.Get("NOMSTUDE")
 Dim CODSCUOL As String = colroot.Get("CODSCUOL")
Next

I think the problem is here:

B4X:
    Dim res As String
       res = Job.GetString
       Log("Back from Job:" & Job.JobName )
       Log("Response from server: " & res)
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Response from server: {}
Doesn't this mean you're receiving an empty MAP from the server?
Check the ws parameters and additionally manage an eventual empty response from the server
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
yes, is an empty map I suppose.....

tried, using this:
B4X:
    Log("http://myip.it:3015/special/GetAlunni?USERWEB='"&Main.USERWEB.Trim&"'&PWDUWEB='"&Main.PWDUWEB.Trim&"'&DBNAME='"&Main.DBNAME.Trim&"'")

putting the string in a browser returns me the JSON filled with records:

[{"CODSTUDE":"0000010028","COGSTUDE":"BARBETTA","NOMSTUDE":"GABRIELE","DTNASSTU":"","CODSCUOL":"_NESSUNA","DESSCUOL":"NON FREQUENTANTE","CLASTUDE":"","SEZSTUDE":"","VALBUONI":".00","DESDIETA":"NORMALE"},{"CODSTUDE":"0000013231","COGSTUDE":"BARBETTA","NOMSTUDE":"ALESSANDRO","DTNASSTU":"","CODSCUOL":"GARIB. E","DESSCUOL":"GARIBALDI ELEMENTARE","CLASTUDE":"3","SEZSTUDE":"B","VALBUONI":"-35.00","DESDIETA":"NORMALE"}]
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
both are possible.

i think is simpler for me to change the parser, how can do that?
and why the other ws is working? maybe because has only 1 record?

and another question, why the res is empty?
B4X:
       Dim res As String
       res = Job.GetString
       Log("Back from Job:" & Job.JobName )
       Log("Response from server: " & res)
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is working if the ws returns a list. No matter 1 or more entries.
If the ws returns {} it can not be a list.

Best is to change the ws and be more consequent in the results. ALWAYS return a LIST. No matter if one entry or 100.
The parser will work if the list is correct. No need to change it.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Don,
what's strange is that (post#5) from a browser returns a list of maps, as expected, while the GET from code returns an empty MAP.
Maybe the problem lies in the parameters passed to ws GetAlunni and the webserver returning an empty map as a way to signal error?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Hi Don,
what's strange is that
i did not read everything... You may be right... The result in B4A should be the same as in browser.

Away from that it is more save if the ws is more STRICT/CONSEQUENT in the results.... Map or List of Maps.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Hi Don,
what's strange is that (post#5) from a browser returns a list of maps, as expected, while the GET from code returns an empty MAP.
Maybe the problem lies in the parameters passed to ws GetAlunni and the webserver returning an empty map as a way to signal error?

I can send the complete ws link (the ip instead of myip) and you can see the result in browsers.
the parameters are correct, because I copied the log output and paste it in the browser.

The strange thing is that I'm getting an empty return from

B4X:
Dim res As String
       res = Job.GetString
       Log("Back from Job:" & Job.JobName )
       Log("Response from server: " & res)

and in that moment I'm not starting to parse the Json, but it should only show the ws return.....
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upload a small project which shows the issue.
I can send the complete ws link (the ip instead of myip) and you can see the result in browsers.
the parameters are correct, because I copied the log output and paste it in the browser.
The log output from what?
Dim res As String
res = Job.GetString
Log("Back from Job:" & Job.JobName )
Log("Response from server: " & res)
isn´t the result what you wrote it would be ion the log

Upload a project and we can have a look.
Im´pretty sure a parameter does not match if you do not get any result. Or maybe the problem is in the ws (my hourglas is broken, sorry)

- Post a complete url which is working in a browser
- Post the B4A code you are using to call this url.
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
try this:

http://comune.refezionescolastica.i...='14184'&PWDUWEB='WEBBEBIE'&DBNAME='FS_SCHOOL'

this is the log from what it's copied:

B4X:
    Dim ws As String
   ws = "http://comune.refezionescolastica.it:3015/special/GetAlunni?USERWEB='"&Main.USERWEB.Trim&"'&PWDUWEB='"&Main.PWDUWEB.Trim&"'&DBNAME='"&Main.DBNAME.Trim&"'"
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   job1.Download(ws)
   Log(ws)

you can substitute the parameters variables with the ones in the link
the jobdone sub is the one posted above
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim ws As String
ws = "http://comune.refezionescolastica.it:3015/special/GetAlunni?USERWEB='"&Main.USERWEB.Trim&"'&PWDUWEB='"&Main.PWDUWEB.Trim&"'&DBNAME='"&Main.DBNAME.Trim&"'"
Dim job1 As HttpJob
job1.Initialize("Job1", Me)
job1.Download(ws)
Log(ws)
The code can not work. You are using special schars here where not needed

Use parametrized Downloadcall

Something like

B4X:
Dim ws As String
   ws = "http://comune.refezionescolastica.it:3015/special/GetAlunni"
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   job1.Download2(ws,"USERWEB",Main.USERWEB.Trim,"PWDUWEB",Main.PWDUWEB.Trim,"DBNAME",Main.DBNAME.Trim)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
tried also, now using local variables instead from globals (main.)

B4X:
    Dim ws As String
   ws = "http://comune.refezionescolastica.it:3015/special/GetAlunni"
   Dim job1 As HttpJob
   job1.Initialize("Job1", Me)
   job1.Download2("http://comune.refezionescolastica.it:3015/special/GetAlunni", _
       Array As String("USERWEB",USERWEB.Trim,"PWDUWEB",PWDUWEB.Trim,"DBNAME",DBNAME.Trim))
   Log(ws)

no way! I can try without the ' but the other ws (GetUser) works like a charm.......
 
Upvote 0
Top