Android Question OKHttp Timeout (connectTimeout - writeTimeout - readTimeout - callTimeout)

bridge1

Member
We would like some clarification on the use of relative timeouts used in OkHttp.

The client has the following 4 settable timeouts:
a. connectTimeout (default 10 seconds)
b. writeTimeout (default 10 seconds)
c. readTimeout (default 10 seconds)
d. callTimeout

From the B4a documentation, set timeouts only on the http request object.

The Okhttp library B4a wrapper sets the following timeouts
connectTimeout, writeTimeout, readTimeout defaults to 30 seconds.

We are creating an Android app, which must send data in POST to REST web services and the size of the data to be sent in a single request can be considerable, being on it can even exceed 2MB for each request.

We are using the latest version of the B4a library "okhttp-4.9.0.jar", and reading the http client configuration parameters it results in a 30 second timeout, but the client raises the timeout after 10 seconds.

From this message
you can view the code to customize the required timeouts, but using it the compiler reports the following error:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, is First = true **
Cannot get methods of class: okhttp3.OkHttpClient$Builder, disabling cache.
Error occurred online: 81 (HTTPClient)
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 java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
for example.http.client.httpclient._settimeout(httpclient.java:102)
at example.http.client.main._activity_create(main.java:405)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:146)
at example.http.client.main.afterFirstLayout(main.java:105)
at example.http.client.main.access$000(main.java:17)
at example.http.client.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5258)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735)



How can we set the timeouts in the subject of this message?

Thank you.
 

drgottjr

Expert
Licensed User
Longtime User
you should be able to increase the timeout(s). before suggesting how you might accomplish that, i need to see what you've already done. please post the code relating to connecting to the server and uploading the data. not a description, the code.
 
Last edited:
Upvote 0

bridge1

Member
Call example

ExecuteJobs:
Private Sub ExecuteJobs(pLink As String, pData() As Byte)
    
    Private oRequest As HttpJob    
    oRequest.Initialize("",Me)        
    oRequest.PostBytes(pLink, pData)
    oRequest.GetRequest.SetContentType("image/png")
    oRequest.GetRequest.SetHeader("auth", "123456")
    oRequest.GetRequest.Timeout = 50000    
    
    Wait For (oRequest) JobDone(oResponse As HttpJob)
    If oResponse.Success Then
        Log("yes")
    Else
        Log("no")
    End If
    oResponse.Release
        
End Sub
 
Upvote 0

bridge1

Member
Performing further tests, setting only the timeout present on the request, it would appear that the enforcement of the timeout takes effect.

Let's hope for confirmation from the community.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, if confirmation from 1 member of the community is enough, i hereby confirm. that is to say, it (GetRequest.Timeout )works for me as well. for what it's worth, a server may have other ideas regarding timeouts. they could supercede your client's notion.
 
Last edited:
Upvote 0
Top