B4J Question [Solved] ParseJSON not called from jobdone.

Mark Read

Well-Known Member
Licensed User
Longtime User
I have been looking at this for hours and cannot see my mistake. There are two http jobs: weather and heating. After each job the resulting data is sent to the ParseJSON. The first job is weather and runs okay but not the parse sub. The second job heating runs correctly but then runs again with the data from weather.

1666782818252.png


I have tried making the sleep longer but it still does not work.
A small pointer would be great. Thanks.
 

Attachments

  • Heating2.zip
    6.3 KB · Views: 68

stevel05

Expert
Licensed User
Longtime User
GetData is a resumablesub you need to "Wait For" it.

I can't test it as it won't run but the relevant subs should be something like:

B4X:
private Sub ConstructURL
    lblStatus.Text="Fetching weather data ..."
    ChannelNumber=320990
    LinkURL=$"https://api.thingspeak.com/channels/${ChannelNumber}/feeds.json?results=${nResults}"$
    MyJobName="Weather"
    Wait For (GetData(LinkURL)) Complete (Resp As String)
    Log(Resp)
    'Sleep(3000)
  
    Log("******************************************")
    lblStatus.Text="Fetching heating data ..."
    ChannelNumber=1325681
    LinkURL=$"https://api.thingspeak.com/channels/${ChannelNumber}/feeds.json?results=${nResults}"$
    MyJobName="Heating"
    Wait For (GetData(LinkURL)) Complete (Resp As String)
    Log(Resp)
    'Sleep(2000)
  
    PrepareChart
End Sub

private Sub GetData(GetLink As String) As ResumableSub
    Log("Getting data from job: " & MyJobName)
    Log(GetLink)
    Dim j As HttpJob
    j.Initialize(MyJobName, Me)
    j.Download(GetLink)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        'Log(j.GetString)
        Log($"Job ${j.JobName} was a success"$)
        ParseJSON(j.GetString)
        j.Release
        Return j.JobName & " parsed"
    Else
        j.Release
        Return j.JobName & " failed"
    End If
  
End Sub

You shouldn't need the sleep statements then either.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Brilliant, now it works many thanks. I had tried with a wait for but could not get the syntax correct.
 
Upvote 0
Top