Android Question How to set the default HTTP connection timeout

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Hi,
I can set the HTTP connection timeout for a call with Job1.GetRequest.Timeout
But I have to do this on every call.

Is there a way to set the timeout globally?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can make a sub that replaces the call for Download. For example:
B4X:
Public Sub Download(Callback As Object, Link As String) As HttpJob
Dim j As HttpJob
j.Initialize("", Callback)
 j.Download(Link)
j.GetRequest.Timeout = 60000
Return j
End Sub

Usage:
B4X:
Dim j As HttpJob = B4XPages.MainPage.Download(Me, "https:...")
Wait For (j) JobDone(j As HttpJob)
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
See:
Sertup de timeOut connect, read, llamada

B4X:
Sub OkHttpTimeout
    
    Dim connTimeOut As Long = 5000
    Dim readTimeOut As Long = 2000
    Dim callTimeOut As Long = 2000
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    
    Dim TimeUnit As JavaObject
    TimeUnit.InitializeStatic("java.util.concurrent.TimeUnit")
    Dim Milliseconds As JavaObject = TimeUnit.GetField("MILLISECONDS")
    
    builder.RunMethod("connectTimeout", Array(connTimeOut, Milliseconds))
    builder.RunMethod("readTimeout", Array(readTimeOut, Milliseconds))
    builder.RunMethod("callTimeout", Array(callTimeOut, Milliseconds))
    jo.SetField("client", builder.RunMethod("build", Null))
    
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi @TILogistic
Do you call sub from post #7 above at app start or before any http call?

TIA
 
Upvote 0
Top