Through Crashlytics I get the following error from one of my apps
Crashlytics:
JSONParser.java line 48
anywheresoftware.b4a.objects.collections.JSONParser.NextObject
Fatal Exception: org.json.JSONException
End of input at character 0 of
Code:
Dim parser As JSONParser
parser.Initialize(FileText)
Dim Map1 As Map = parser.NextObject '<-- Error in this line according to Crashlytics
I understand that the problem (End of input at character 0 of) occurs because the JSON file was not created correctly, this JSON I generate with this event
B4XMainPage:
Sub B4XPage_Background
...
Private JSONGenerator As JSONGenerator
JSONGenerator.Initialize(MapaDatos)
JS = JSONGenerator.ToPrettyString(2)
File.WriteString(Path, FileText, JS)
End Sub
Should I use a Try...Catch before ending B4XPage_Background or the problem is another
In principle it should never be empty since I load the JSON and then I create the file, it is not possible to save the file if there is no JSON data and then before opening the JSON I verify that the file exists
For Each v As B4XView In pnl.GetAllViewsRecursive
...
Private JSONGenerator As JSONGenerator
JSONGenerator.Initialize(MapaDatos)
JS = JSONGenerator.ToPrettyString(2)
File.WriteString(Folder, FileName, JS)
Next
Maybe I should put the last line (File.WriteString...) outside of For Each... Next, like this:
B4X:
Private JSONGenerator As JSONGenerator
Dim TextJS As String = ""
For Each v As B4XView In pnl.GetAllViewsRecursive
...
JSONGenerator.Initialize(MapaDatos)
JS = JSONGenerator.ToPrettyString(2)
Next
File.WriteString(Folder, FileName, JS)
I think I don't explain me well, this is the code:
now:
Dim JS As String
Dim MapaDatos As Map
Dim LstMapa As List
MapaDatos.Initialize
LstMapa.Initialize
For Each v As B4XView In pnl.GetAllViewsRecursive
'...code
'... load LstMapa
MapaDatos.Put("lista", LstMapa)
Private JSONGenerator As JSONGenerator
JSONGenerator.Initialize(MapaDatos)
JS = JSONGenerator.ToPrettyString(2)
File.WriteString(Folder, FileName, JS)
Next
I can File.WriteString move after the loop, like this?
B4X:
Dim JS As String
Dim MapaDatos As Map
Dim LstMapa As List
MapaDatos.Initialize
LstMapa.Initialize
For Each v As B4XView In pnl.GetAllViewsRecursive
'...code
'... load LstMapa
MapaDatos.Put("lista", LstMapa)
Private JSONGenerator As JSONGenerator
JSONGenerator.Initialize(MapaDatos)
JS = JSONGenerator.ToPrettyString(2)
Next
File.WriteString(Folder, FileName, JS) ' <-- AFTER THE LOOP
The other question is, Should I include a Wait for in B4XPage_Background as suggested by aeric