Android Question JSON Parsing Problem

Joydeep Das

New Member
Licensed User
When I try to connect the remote mysql database, it fatches the data (shows in Log) but the data can not be parsed. Shows the following error.


Response from server:
"Connected first"
~l11851975:
~l11851975:
~l11851975:
[{"user_name":"joydeepi","pass_word":"*123#Joy"},{"user_name":"AA","pass_word":"BB"}]


main_jobdone (java line: 428)
java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
at b4a.example.main._jobdone(main.java:428)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1164)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Few days earlier, I was able to parse the data but from yesterday this error message shows. I have formated the desktop and then reinstalled everything. But the problem remains.

Please Help me .....
 

Joydeep Das

New Member
Licensed User
I have tried this. My JpbDone Code is given below:

Sub JobDone(Job As HttpJob)
ProgressDialogHide
If Job.Success=True Then
Dim res As String


res = Job.GetString
Log("Response from server: " & res) 'Upto this is correct


Dim parser As JSONParser
parser.Initialize(res)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim user_name As String = colroot.Get("user_name")
Dim pass_word As String = colroot.Get("pass_word")


Log(user_name)
Log(pass_word)
Next


Else
Log(Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Log("Response from server: " & res) 'Upto this is correct
No your response is not just a JSON array. Your response is actually (at least to your initial post):
"Connected first"
~l11851975:
~l11851975:
~l11851975:
[{"user_name":"joydeepi","pass_word":"*123#Joy"},{"user_name":"AA","pass_word":"BB"}]
As you see, your server returns a bunch of items before your JSON array. So your variable res does not just contain
[{"user_name":"joydeepi","pass_word":"*123#Joy"},{"user_name":"AA","pass_word":"BB"}]
But
"Connected first"
~l11851975:
~l11851975:
~l11851975:
[{"user_name":"joydeepi","pass_word":"*123#Joy"},{"user_name":"AA","pass_word":"BB"}]
And that's the string your feeding to
Dim parser As JSONParser
parser.Initialize(res)
Dim root As List = parser.NextArray
And therefore you end up with
java.lang.RuntimeException: JSON Array expected.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…