JSON error

danoptic

Member
Licensed User
Longtime User
I send a sql request and I get:

Response from server: [{"id":"1","name":"daily"},{"id":"2","name":"2_weeks"},{"id":"3","name":"monthly"},{"id":"4","name":"3_months"},{"id":"5","name":"year"}]

my code is:

Dim parser As JSONParser
parser.Initialize(res)
Dim l As List
l = parser.NextArray
For i = 0 To l.Size- 1
Dim m As Map
m = l.Get(i)
spnCLCompany.Add(m.Get("name"))
Next

but I get the error:

java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:59)
at danoptic.onlinestore.choosecl._hc_responsesuccess(choosecl.java:541)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA$3.run(BA.java:296)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: JSON Array expected.
java.lang.RuntimeException: Failed to register input channel. Check logs for details.
at android.view.InputQueue.nativeRegisterInputChannel(Native Method)
at android.view.InputQueue.registerInputChannel(InputQueue.java:92)
at android.view.ViewRoot.setView(ViewRoot.java:572)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at anywheresoftware.b4a.Msgbox.msgbox(Msgbox.java:135)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:210)
at anywheresoftware.b4a.BA$3.run(BA.java:296)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)



what I m doing wrong !!!:sign0085:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The JSON data is indeed made of an array as the root element. I recommend you to save the response as a text file and open it with a tool such as Notepad++. Maybe there is an extra character or UTF8 BOM marking at the beginning. If you don't find the problem then please upload the response (as a file) to this thread.
 
Upvote 0

danoptic

Member
Licensed User
Longtime User
output file + php code

I see a ? at the begining of the string !!

I copy the php code that send it back
what is that?
How can I remove it?

<?

include("dbinfo.inc.php");

mysql_connect($localhost,$username,$password);
@mysql_select_db($database) or die("unable to select database");

$query = file_get_contents("php://input");
$sth = mysql_query($query);

if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
 

Attachments

  • TASKBRAND.txt
    231 bytes · Views: 201
Upvote 0

danoptic

Member
Licensed User
Longtime User
Temporary solution

I deal whith this problem by copying (substring) what I need but how can I resolve that issue?
 
Upvote 0
Top