B4J Question HTTPJob & ResumableSub - Retry on fail request

aaronk

Well-Known Member
Licensed User
Hi,

I am using the ResumableSub in my project with a HTTPJob, and it will then return the request back to the Sub like the following (as an example):

B4X:
Sub RunMe
    
    Dim rs As ResumableSub = GetPage
    Wait For(rs) Complete (Result As String)
    
    Log(Result)
    
End Sub

Sub GetPage As ResumableSub

    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.google.com")
    Wait For (j) JobDone(j As HttpJob)
    
    Dim ValueToReturn As String
    
    If j.Success Then
            ValueToReturn = j.GetString
        Else
            Log("error")
    End If
    
    j.Release
    Return ValueToReturn

End Sub
Let's say in the ResumableSub 'GetPage' it fails to get the value from the page for some reason. How do I make the ResumableSub do the request again, so it can try and get the value again and return a value to the original request in the RunMe sub ?
 

Erel

Administrator
Staff member
Licensed User
If there is a valid internet connection then the request will work. If there is no valid connection then it will fail until there is a connection. What I'm trying to say is that in most cases retrying to send the request will not help much.

You can do something like:
B4X:
Sub GetPage As ResumableSub
 For i = 1 To 10
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.google.com")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
            Dim res As String = j.GetString
            j.Release
            Return res
    Else
            Log("error: " & i)
    End If    
    j.Release
   Sleep(3000)
 Next
Return ""
End Sub
 

KMatle

Expert
Licensed User
It's more about the business logic. You can start a job whenever you like (simple object). So it's better to return "failed" to the business logic and then decide there what to do (try again now, later or whatever). E.g. you want to insert a new user to a remote db via OkHttpUtils. Then it's important to save the data in a file or so for later (or mark it as to be uploaded later).
 

aaronk

Well-Known Member
Licensed User
If there is a valid internet connection then the request will work. If there is no valid connection then it will fail until there is a connection. What I'm trying to say is that in most cases retrying to send the request will not help much.
I was finding when I was sending the request to the remote server (remote server not handled by me), it was returning success = False for the HTTPJob.
Error message was showing: The server cannot service the request because the media type is unsupported.

If I send the request again the second time it worked fine.

So I was adding, if it failed the first time then try and send it again, and if it fails the second time then give up and notify the user.

Strange thing is it doesn't happen all the time.
 
Top