Android Question JSON Object expected.

tufanv

Expert
Licensed User
Longtime User
Hello,

My app is working on a requesting data from a webserver with http request and return json as response.
Some crash reports show this error :

Exception java.lang.RuntimeException: JSON Object expected.
anywheresoftware.b4a.objects.collections.JSONParser.NextObject (JSONParser.java:50)
ct.canlidoviz.main._jobdone (main.java:1311)

Line that is equal to main.java 1311 is :

B4X:
If Job.JobName="songrafik" Then
        If Job.Success = True Then
            Log(Job.GetString)
            Dim parser As JSONParser
            parser.Initialize(Job.GetString)

As I can understand from the percentage of error ( it is around %5 of all users ) most of the time there is no problem , but I want to fix this. If the job is success , I must have get the json also because it is impossible to get a blank response from the server. This error happens when the received response is not json so to fix this ,

Can I use for example try catch for the above code and if stg is catched repeat this job ?

TY
 

DonManfred

Expert
Licensed User
Longtime User
What about checking the length of the result before using jsonparser????
What is the result when it fails? what do you get with your
B4X:
Log(Job.GetString)
?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
What about checking the length of the result before using jsonparser????
What is the result when it fails? what do you get with your
B4X:
Log(Job.GetString)
?
I normally get stg like this :

B4X:
{"status":true,"data":[{"value":"4.62195","date":"2018-06-27 05:49:59"},{"value":"4.62095","date":"2018-06-27 06:22:58"},{"value":"4.62245","date":"2018-06-27 06:55:58"},{"value":"4.62275","date":"2018-06-27 07:28:59"},......

Yes , I was thinking that , if i check if the job.getstring.length is less then 10 for example , and if it is less than 10 repeat the job request , I would fix the problem, it is better than try catch ..
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Can I use for example try catch for the above code and if stg is catched repeat this job ?
With Wait For, that could be easier implemented than using JobDone.
B4X:
'
   Do While retry = True
       Dim j As HttpJob
       j.Initialize("", Me)
       j.Download("https://www.google.com")
       Wait For (j) JobDone(j As HttpJob)
       If j.Success Then
           Log(j.GetString)
           Dim parser As JSONParser
           Try
               parser.Initialize(j.GetString)
               retry = False
           Catch
               Log(LastException)
           End Try
       End If
       j.Release
   Loop
Note: Just noticed that this will repeat constantly if j.Success is False. That may or not may not be the intended purpose and the code may need to be adjusted. Plus, you don't have to use try/catch (should have read follow up posts better), but then this can be adapted to whatever is used to determine if the returned value (j.GetString) is acceptable.
 
Last edited:
Upvote 0
Top