Android Question OkHttp3 timeout (again)

udg

Expert
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 )
B4X:
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
    TimeUnit.InitializeStatic("java.util.concurrent.TimeUnit")
    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:
B4X:
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(JavaObject.java:363)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
    at ch.mnog.c4lunch.pgprenota._vvvvvvvvvvvvvvvvv7(pgprenota.java:769)
    at ch.mnog.c4lunch.pgprenota$ResumableSub_UploadOrdine.resume(pgprenota.java:977)
    at ch.mnog.c4lunch.pgprenota._vvvvvvvvvvvvvvvvvv1(pgprenota.java:944)
    at ch.mnog.c4lunch.pgprenota$ResumableSub_btnOrdina_Click.resume(pgprenota.java:361)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1772)
    at android.os.Handler.handleCallback(Handler.java:836)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6251)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
It sounds a bit strange since Builder definitely has a "connectTimeout" method. See here.

So, why it crashes? TIA
 

udg

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

drgottjr

Expert
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
Top