I'm using this Chinese device PDA3505 which is android 6.0 and have written a kotlin application for it. To print something (it has a thermal printer) it needs to fetch print data from the server, which in hindsight works normally on Wi-Fi but when I switched over to the local mobile internet it started giving me SocketTimeoutExceptions. (and it has to be on the local mobile internet)
The code used to create OkHttpClient:
private var httpClient: OkHttpClient = OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.callTimeout(30, TimeUnit.SECONDS)
.pingInterval(15, TimeUnit.SECONDS)
.build()
(Increasing the 30seconds and 15seconds doesn't change the behaviour)
The code used to call the API:
val url = apiUrl + urlPath
val request: Request = if (!withHeaders) {
Request
.Builder()
.url(url)
.post(postBody)
.build()
} else {
Request
.Builder()
.url(url)
.post(postBody)
.addHeader("Authorization", getAuthToken())
.build()
}
try {
val response = httpClient.newCall(request).execute()
if (response != null) {
return if (response.isSuccessful) {
val reqBody: String? = response.body?.string()
if (reqBody != null && reqBody != "" && reqBody != "null") {
JSONObject(reqBody)
} else {
null
}
} else {
null
}
}
} catch (ex: SocketTimeoutException) {
println(ex)
} catch (e: Exception) {
println(e)
}
return null
it takes a long time to process, and yes the local mobile internet in this device is slow. (it doesn't have 4G and its the only option I have). The best I found here was here -> Related Question, but I can't make anything of it or if its possible to extend tcp_socket connection time. As for the API server, I have tested it and the maximum response time is 0.3s (most traffic on this API would be 10 user/hour) so I'm thinking the API isn't at fault.
Is there anyway to confirm that sockets have a certain time? and can I extend that time? or is the problem something else and I'm not seeing it? help pls
I Tried to extend the OkHttpClient timeouts but to no avail. It works on Wi-Fi but fails and gives SocketTimeoutException on local Mobile internet which is 3G (that's the maximum this device can connect to)