Android Question Http Job Wait For Hangs

stingrae

Member
Licensed User
Longtime User
Hi,

I can't seem to figure this out. When I call the following code, it hangs on the Wait For:
B4X:
Dim strThisURL As String = "URL of my API"
Dim j As HttpJob
j.Initialize("" , Me)
j.Download(strThisURL)
           
Wait For (j) JobDone(j As HttpJob)  ' Hangs Here

Dim MyDataList As List
Dim parser As JSONParser
Dim response As String = j.GetString
parser.Initialize(response)

But, if I run it like I have the other API calls in my app, with the result going to the

B4X:
Sub JobDone (Job As HttpJob)
 :
 :
End Sub

sub, then it works perfectly. I could run it that way, but I like the idea of waiting for it in the same sub.
Can anyone see what I'm doing wrong?
 

JohnC

Expert
Licensed User
Longtime User
What version of OkHttpUtils2 are you using?

If you are not using the latest OkHttpUtils version 2.82, then try using it and see if it fixes this issue.
 
Upvote 0

stingrae

Member
Licensed User
Longtime User
Unfortunately I'm on 2.82
I did a clean and uninstall-reinstall of the App too but same result I'm afraid. I just hangs at that line.
In the debug console window is the text:

SubmitJob (httputils2service):42

Not sure if that helps. There is nothing in the Logs window.
 

Attachments

  • okhttp.jpg
    okhttp.jpg
    7.2 KB · Views: 309
Upvote 0

JohnC

Expert
Licensed User
Longtime User
What is the maximum time you waited for it to complete? I ask because I don't know what the "timeout" period is, but it might be 60-120 seconds. And I'm thinking it should at least eventually timeout and report an error.

The other possibility is that the job is completing before it even enters the "Wait For" line - and this can cause everything to hang on the "Wait for" line because the event already happened and wont happen again, so the wait for will wait forever. You can see if this is the case by first checking the J.Success value to see if its true (meaning the job already completed) and checking to see if .ErrorMessage contains anything (meaning it threw an error immediately) before the "Wait For" line to see if it did actually complete the job before it hits the Wait For line.
 
Last edited:
Upvote 0

stingrae

Member
Licensed User
Longtime User
I've waited for minutes but it doesn't go anywhere or do anything. And when I call the API from the browser, or going to a separate sub then it's a split second.

I also just tried your idea of checking for j.Success as per the code below. I stepped through it and it was False and then went into the IF TRUE bit, and then hung on the Wait For again.

B4X:
If j.Success <> True Then
    Wait For (j) JobDone(j As HttpJob)
End If
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Did you also see if there was any message in the J.ErrorMessage just in case the job did complete, but failed (.success = false)?
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
Longtime User
B4X:
If j.Success <> True Then
    Wait For (j) JobDone(j As HttpJob)
End If

Correct code must be:
B4X:
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release

BTW, Are you releasing your job anywhere before you use it again?
 
Upvote 0

stingrae

Member
Licensed User
Longtime User
I've updated now to 2.90, thank you @kisoft for pointing that out. But same result I'm afraid. :(

@Jorge M A - in the first post you'll see that I had that, but it never gets past:
B4X:
Wait For (j) JobDone(j As HttpJob)

Here's the full code with the variables:

B4X:
    Dim Map1 As Map
      
    Map1.Initialize
    Dim strThisURL As String = "xyz"
    Log(strThisURL)
    Dim j As HttpJob
    j.Initialize("" , Me)
    j.Download(strThisURL)
          
    Wait For (j) JobDone(j As HttpJob)

    Dim MyDataList As List
    Dim parser As JSONParser
    Dim response As String = j.GetString
    parser.Initialize(response)

     ' Read & process the data

    j.Release

Can anyone please try and let me know if it works for them? Thanks in advance.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Did you check the J.ErrorMessage to see if it has any text in it?
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
B4X:
Sub btnGo1_Click
    
    Dim strThisURL As String = ServerUrl & "arnCompetition?BusinessID=1&DateFrom=2010-01-01&DateTo=2100-01-01"
    Log(strThisURL)
    Dim j As HttpJob
    j.Initialize("" , Me)
    j.Download(strThisURL)
    
    Wait For (j) JobDone(j As HttpJob)
    Log(j.GetString)
    Dim parser As JSONParser
    parser.Initialize(j.GetString)
    Dim root As List = parser.NextArray
    For Each colroot As Map In root
        Dim StartDate As String = colroot.Get("StartDate")
        Log(StartDate)
        Dim Competitors As String = colroot.Get("Competitors")
        Log(Competitors)
        Dim ID As Int = colroot.Get("ID")
        Log(ID)
        Dim Name As String = colroot.Get("Name")
        Log(Name)
    Next
    j.Release
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not sure what you mean with "hangs" however your program works as expected.

Put a breakpoint after the Wait For line and you will see that it is reached.

Maybe you expect that JobDone sub will be raised. This is not the case as you are intercepting the event inside the sub.

Tip: you should never have a JobDone sub.
 
Upvote 0
Top