Android Question OkHttp3 timeout (again)


Licensed User
Longtime User
Hi all,
I experimented a long wait for a PostBytes so investigated a bit more about timeouts.
The simplest method of making use of HttpJob.GetRequest.Timeout worked ok for the case in hand.
But I found other interesting code on the Forum and would like to understand it better (and eventually use it whenever I would need it).
The code I found (author @TILogistic )
Sub OkHttpTimeout
    Dim connTimeOut As Long = 5000
    Dim readTimeOut As Long = 3000
    Dim callTimeOut As Long = 3000
    Dim jo As JavaObject = HttpUtils2Service.hc
    Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
    Dim TimeUnit As JavaObject
    Dim Milliseconds As JavaObject = TimeUnit.GetField("MILLISECONDS")
    builder.RunMethod("connectTimeout", Array(connTimeOut, Milliseconds))      '<-- breaks here
    builder.RunMethod("readTimeout", Array(readTimeOut, Milliseconds))
    builder.RunMethod("callTimeout", Array(callTimeOut, Milliseconds))
    jo.SetField("client", builder.RunMethod("build", Null))
End Sub
In order to access the hc object I had to put the HU2_PUBLIC symbol in the Project/Build Configurations.
I'm not sure if I've to call this sub each time I want a PostBytes operation with those timeouts or it is enough to do it once, at program start, and it will be applied to every operation.
Anyway, when it comes to the line which sets the "connectTimeout" the program stops with the following error:
Cannot get methods of class: okhttp3.OkHttpClient$Builder, disabling cache.
pgprenota_vvvvvvvvvvvvvvvvv7 (java line: 769)
java.lang.RuntimeException: Method: connectTimeout not found in: okhttp3.OkHttpClient$Builder
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(
    at anywheresoftware.b4j.object.JavaObject.RunMethod(
    at ch.mnog.c4lunch.pgprenota._vvvvvvvvvvvvvvvvv7(
    at ch.mnog.c4lunch.pgprenota$ResumableSub_UploadOrdine.resume(
    at ch.mnog.c4lunch.pgprenota._vvvvvvvvvvvvvvvvvv1(
    at ch.mnog.c4lunch.pgprenota$ResumableSub_btnOrdina_Click.resume(
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent(
    at anywheresoftware.b4a.keywords.Common$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
It sounds a bit strange since Builder definitely has a "connectTimeout" method. See here.

So, why it crashes? TIA


Licensed User
Longtime User
Unfortunately, not.
Tried without obfuscation and it resulted in the same error
Upvote 0


Licensed User
Longtime User
the problem is in line 1:
Cannot get methods of class: okhttp3.OkHttpClient$Builder, disabling cache.
not further down. the sub works; you did something to make it not work...

beyond that, if you're using the same client, you don't have to run the snippet more than once. unless you change timeouts
Upvote 0