B4J Question HTTP Message Fails to Send

lip

Active Member
Licensed User
Longtime User
This is a NonUI Raspberry Pi App

I send various HTTP messages to three different servers. Some of the requests work every time but others work about 3 out of 4 times. I use timers to try and ensure that I never have more than one HTTP request waiting at any one time, but I get the feeling the failures might be down to me sending a request to one server too quickly after receiving a Success response from the previous request, which may have been from a different server. The failure messages are (I think) coming from the Pi and not the Server and look like this:-

B4X:
19:21:29 Post: http://www.**********.com/CommGet.aspx?Originator=NetLinks&Postcode=******&Collator=51341&LastResponseID=1944&LastResponseString=NetLinks,Collator,NetLinksReadInRange,1944,29
java.io.InterruptedIOException: timeout
    at okio.AsyncTimeout.exit(AsyncTimeout.java:258)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:215)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
    at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:917)
    at com.squareup.okhttp.internal.http.HttpEngine.access$300(HttpEngine.java:95)
    at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:902)
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:760)
    at com.squareup.okhttp.Call.getResponse(Call.java:274)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
    at com.squareup.okhttp.Call.execute(Call.java:81)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

lip

Active Member
Licensed User
Longtime User
If the request is going to respond then it does so within 1 or sometimes 2 seconds. This timeout message comes back after 30 seconds which is the default timeout, I haven't changed it.

I have three things going on: (i) A Long Poll for commands (our server cannot handle websockets) (ii) HTTP Messages to synchronize a database (these are the messages that occasionally time-out and (iii) Bluetooth messages to control local sevices.

My strategy is to call a Long Poll with a 30 second timeout; if the server has no commands then it sends a positive "Timed Out" message back. I then alternate between checking the Bluetooth device for any incoming information and calling HTTP Sync Posts to sync the database.

I already use JobDone as you suggest to send the next message until all synching is complete, then revert to Long Polling.

Each time I Long Poll or HTTP Post or Bluetooth Fetch, I also set a TimeOut Timer with a slightly longer timeout than the network timeout. These Blocksing Timers will either be disabled by JobDone or, in the event that JobDone does not fire, the Timer's Tick event. In my main Loop Timer, which synchronises events and prioritises different functions, I aways check that all three timers are NOT enabled before I all any other asynchronous event. This means that I am only ever waiting for one async event at a time, which generally stops things getting in a muddle. From my experience, if a make any HTTP Post before the previous one has JobDone, I start to get a lot of timeouts.

I am slightly concerned that if the processor (message queue?) is very busy, the Timers might be starting a while before the next line of code actually runs, and so they appear to timeout before the code has had a chance to process. is this possible?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am slightly concerned that if the processor (message queue?) is very busy, the Timers might be starting a while before the next line of code actually runs, and so they appear to timeout before the code has had a chance to process. is this possible?
It is not possible. The tick event will only be raised when the main thread is free to process the message queue. This will only happen after it completes running the current sub (or subs).
 
Upvote 0
Top