How to handle Okhttp3 POST Failing after changing location? Roaming issue?

24 Views Asked by At

Issue: A user typically makes a few POSTs successfully from one room and then walks a few minutes to another location. During this walk it is possible that the device roams and tries to connect to other networks, but still shows that it is connected to the "main network" and has a decent wifi strength. At their new location they try to POST and it fails. Typically, they move or walk around and retry their POST. Sometimes after a few minutes it will be successful. Other times, even with a device restart it won't go through. Typically, over a long period of time it will become usable again.

The only workaround we have found is that the user POSTS from their first destination and then shuts off the device, walks to the second location, turns it back on, and it will POST successfully. However, this is not an ideal solution.

Does anyone have any ideas of why this is happening or how I can modify my okhttp3 timeout implementation to better handle this situation?

Some broken out bits of my code:

        val client = OkHttpClient().newBuilder()
        .connectTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(10, TimeUnit.SECONDS)
        .readTimeout(10, TimeUnit.SECONDS)
        .callTimeout(0, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .build()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                println("Failed")
            }

        override fun onResponse(call: Call, response: Response) {
            if (response.isSuccessful) {
                println("Sync Successful")
            } else {
                println("Sync Failed")
            }
0

There are 0 best solutions below