Android Question Http's Request's Timeout seems not be configured

Hi, I am using library OkHttpUtils2 and find the timeout of Http's request sticked to around 20 seconds but cannot be assigned.

My testing codes is:
[CODE lang="b4x" title="testing code for timeout" highlight="testing code for timeout"]#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private timer As Timer
Private j As HttpJob
Private TIMER_LIMIT As Int = 5
Private REQUEST_LIMIT As Int = 2
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("myLayout")
If Button1.IsInitialized = False Then
Button1.Initialize("Button1")
Activity.AddView(Button1, 10dip,10dip,100dip,100dip)
Button1.Text = "Click"
End If
timer.Initialize("timer", TIMER_LIMIT * DateTime.TicksPerSecond)
LogColor("Timer limit: " & (TIMER_LIMIT * DateTime.TicksPerSecond), Colors.Blue)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Log("+++ Start: " & DateTime.Time(DateTime.Now))
timer.Enabled = True
j.Initialize("", Me)
j.Download("https://www.google.com")
j.GetRequest.Timeout = REQUEST_LIMIT * DateTime.TicksPerSecond
'LogColor("TicksPerSecond: " & DateTime.TicksPerSecond, Colors.Blue)
LogColor("Request limit: " & (REQUEST_LIMIT * DateTime.TicksPerSecond), Colors.Blue)
ProgressDialogShow2("querying...", True)
Wait For (j) JobDone(Job As HttpJob)
Dim isJobSuccess As Boolean = Job.Success
j.Release
If isJobSuccess = False Then
LogColor("Connetion Error: " & CRLF & Job.ErrorMessage, Colors.Red)
End If
Log("is Success? " & isJobSuccess)
ProgressDialogHide
Dim lng2 As Long = DateTime.Now
Log("=== End (JobDone): " & DateTime.Time(lng2))
If timer.Enabled Then
timer.Enabled = False
End If
End Sub

Sub timer_tick
timer.Enabled = False
ProgressDialogHide
Log("~~~ End (Timeout): " & DateTime.Time(DateTime.Now))
If j.IsInitialized Then
j.Release
End If
End Sub[/CODE]

Since the timer limit is greater than request's timeout (i.e. 5 > 2), timeout in jobdone would raise before timer_tick.
However, the timer_tick raise before jobdone.

The Logs are listed:
[CODE title="Log" highlight="Log"]** Activity (testtimeout) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (testtimeout) Create, isFirst = false **
Timer limit: 5000
** Activity (testtimeout) Resume **
+++ Start: 12:42:00
Request limit: 2000
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
~~~ End (Timeout): 12:42:05
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "www.google.com": No address associated with hostname, Response:
Connetion Error:
java.net.UnknownHostException: Unable to resolve host "www.google.com": No address associated with hostname
is Success? false
=== End (JobDone): 12:42:20
[/CODE]

The timer is accurate but timeout of http's request is no use in this case. Is there bug inside library OkHttpUtils2?
 
Hi Erel,

HttpJob should never be a global variable.

I have declared HttpJob inside the function but the timeout still sticked to 20 seconds

[CODE lang="b4x" title="httpjob inside a function" highlight="httpjob inside a function"]Sub Button1_Click
Log("+++ Start: " & DateTime.Time(DateTime.Now))
' timer.Enabled = True
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://www.google.com")
j.GetRequest.Timeout = REQUEST_LIMIT * DateTime.TicksPerSecond
'LogColor("TicksPerSecond: " & DateTime.TicksPerSecond, Colors.Blue)
LogColor("Request limit: " & (REQUEST_LIMIT * DateTime.TicksPerSecond), Colors.Blue)
ProgressDialogShow2("querying...", True)
Wait For (j) JobDone(Job As HttpJob)
Dim isJobSuccess As Boolean = Job.Success
j.Release
If isJobSuccess = False Then
LogColor("Connetion Error: " & CRLF & Job.ErrorMessage, Colors.Red)
End If
Log("is Success? " & isJobSuccess)
ProgressDialogHide
Dim lng2 As Long = DateTime.Now
Log("=== End (JobDone): " & DateTime.Time(lng2))
If timer.Enabled Then
timer.Enabled = False
End If
End Sub[/CODE]

[CODE title="log" highlight="log"]*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (testtimeout) Create, isFirst = true **
Timer limit: 5000
** Activity (testtimeout) Resume **
+++ Start: 10:04:09
Request limit: 2000
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "www.google.com": No address associated with hostname, Response:
Connetion Error:
java.net.UnknownHostException: Unable to resolve host "www.google.com": No address associated with hostname
is Success? false
=== End (JobDone): 10:04:29
** Activity (testtimeout) Pause, UserClosed = false **
[/CODE]

I think the Request's timeout of HttpJob is not working.

The error has nothing to do with the timeout. It looks like the device isn't connected to the internet.
I intended to set the WiFi of mobile phone to a hotspot which has already disconnected to internet because I want to see what happen when the mobile is disconnected to internet.
 
Upvote 0
Hi Erel,
In my situation, the short timeout is critical.
I am developing an application for receiving products.
The mechanism is that the barcode scanned which would be searched in local database (i.e. sqlite) first.
If the barcode is not found in local database, it would be "POST" to API to identify this product.
If api return {"isfound": false} in json format, it is not allowed to receive this product.
Some area of warehouse is found weak WiFi signal. It is difficult to ensure well handheld WiFi signal in anywhere.
Thus, I would add timer in this case to handle time out case.
 
Upvote 0
Back
Top