Android Question How to treat httpjob timeout?

wimpie3

Well-Known Member
Licensed User
Longtime User
I want to detect a timeout with httpjob.

I'm doing this:

B4X:
Dim Job1 As HttpJob
Job1.Initialize(...)
Job1.Poststring(...)
Job1.GetRequest.Timeout =5000 ' 5 seconds
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("succes")
Else
Log("failed")
End If

When I start my app without internet connection, I thought the "failed" message would be shown after 5 seconds. But nothing happens. It seems that Android is trying to do the PostString and "hangs" on that line. What am I doing wrong?
 

udg

Expert
Licensed User
Longtime User
Your HttpJob is named Job1, not simply j
Correct the Wait For statement and it will work
 
Upvote 1

josejad

Expert
Licensed User
Longtime User
It's always a good idea to upload a small project to see the issue.
This example works

B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://www.google.com")
    j.GetRequest.Timeout =5000 ' 5 seconds
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        xui.MsgboxAsync(j.GetString, "OK")
    Else
        xui.MsgboxAsync("Error connecting", "ERROR")
    End If
    j.Release
 

Attachments

  • httputils.zip
    10 KB · Views: 172
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I want to detect a timeout with httpjob.

I'm doing this:

B4X:
Dim Job1 As HttpJob
Job1.Initialize(...)
Job1.Poststring(...)
Job1.GetRequest.Timeout =5000 ' 5 seconds
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log("succes")
Else
Log("failed")
End If

When I start my app without internet connection, I thought the "failed" message would be shown after 5 seconds. But nothing happens. It seems that Android is trying to do the PostString and "hangs" on that line. What am I doing wrong?
use Try Catch and j.Response (know why the error)

1636576625053.png


not internet:
ResponseError. Reason: java.net.UnknownHostException: Host desconocido (xxxxxxxxxx), Response:
test:
B4X:
    Dim j As HttpJob
    Try   
        j.Initialize("", Me)
        j.Download("https://www.google.com")
        j.GetRequest.Timeout = 5000 ' 5 seconds
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            xui.MsgboxAsync(j.GetString, "OK")
        Else
            xui.MsgboxAsync("Error connecting", j.Response.StatusCode) 'error code
        End If
    Catch
        Log(LastException) 'Error general
    End Try
    j.Release
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
When there is no internet, all I get is this message in the log window:
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "www.test.com": No address associated with hostname, Response:

(and yes, the Response: is empty at the end of the line)
  • The timeout parameter is ignored since it takes OVER 5000ms for the error to be displayed
  • JobDone is never fired, so j.success cannot be checked
  • The Try/Catch does not fire
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
JobDone is not used when you use a OkHttpUtils2 with Wait For. You must manage the events in the
B4X:
        If j.Success Then
            xui.MsgboxAsync(j.GetString, "OK")
        Else
            xui.MsgboxAsync("Error connecting", j.Response.StatusCode) 'error code
        End If

Using Wait For we can wait for the JobDone event in the same sub that started the download.
No longer is it needed to have a single sub that handles all requests results.

Features Erel recommends to avoid
Sub JobDone -> Wait For (j) JobDone:
[B4X] OkHttpUtils2 with Wait For
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,
as you may know, standard timeout is set to 30s. If you experience a never ending wait, could it be caused by a firewall/antivirus that block your outgoing message?
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
When there is no internet, all I get is this message in the log window:
ResponseError. Reason: java.net.UnknownHostException: Unable to resolve host "www.test.com": No address associated with hostname, Response:

(and yes, the Response: is empty at the end of the line)
  • The timeout parameter is ignored since it takes OVER 5000ms for the error to be displayed
  • JobDone is never fired, so j.success cannot be checked
  • The Try/Catch does not fire

status OK:
1636628025650.png


status Error (not Internet)
1636628288622.png


upload an example to check it out.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Hi,
as you may know, standard timeout is set to 30s. If you experience a never ending wait, could it be caused by a firewall/antivirus that block your outgoing message?
the timeout may vary and depends on the request timeout setting on the web server.

one possible help is to check with ping:

1636630086213.png
 
Last edited:
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
the timeout may vary and depends on the request timeout setting on the web server.
No no, that can't be true. If we specify there should be a timeout for our httpjob, the timeout should fire when we reach OUR timeout, not the server timeout.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
No no, that can't be true. If we specify there should be a timeout for our httpjob, the timeout should fire when we reach OUR timeout, not the server timeout.
what I am trying to say is that the wait time is not infinite, it can be handled by the client or the server.
 
Upvote 0

wimpie3

Well-Known Member
Licensed User
Longtime User
PROBLEM SOLVED!

A timeout DOES indeed raise JobDone AND sets j.success to false, so there is no issue from the side of B4A! You don't need a try/catch either.

The issue was due to the emulator I'm using: Genymotion.

When the Genymotion emulator starts and there is a network available, and WHILE the emulator is running you cut the network, things go terribly wrong with its internal network driver.

In order to see the timeouts happening, you must start Genymotion with the network already down. In that case, everything works fine.

Thank you all for helping me.
 
Upvote 0
Top