Hi!
I'm having some problems reading a JSON.
I've 2 ws, called GetUser and GetAlunni, this how I consume them:
GetUser
ws response:
[{"USERWEB":"10028 ","PWDUWEB":"WEBBAACI ","DBNAME":"FS_SCHOOL"}]
I'm able to read this Json!
GetAlunni
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:
the Json seems empty, why?
regards,
Ciro
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: