POST fails with Retrofit (connect timed out) but works with Postman [on hold]

Multi tool use
POST fails with Retrofit (connect timed out) but works with Postman [on hold]
SOLVED: stupid mistake, I had https://foo.foo.com:433 instead of https://foo.foo.com:443...
I've implemented Retrofit and I'm trying to send a POST request with JSON data as JsonObject. It's not working and I get a failure "connect time out", but I can send a POST request without problem via Postman. At first I thought it was because of the header, I've tried with an Interceptor and to use @Header instead of @Headers. Thank you for your help.
Postman
Log from HttpLoggingInterceptor()
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO: --> POST https://foo.foo.com:433/
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO: Content-Length: 132
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO:
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO: {"transport":"web","userId":"ggrojeomieje2","moduleId":105,"moduleName":"signal","moduleVersion":1,"operation":"signal","params":{}}
Jun 30, 2018 10:22:09 AM okhttp3.internal.platform.Platform log
INFO: --> END POST (132-byte body)
Jun 30, 2018 10:22:19 AM okhttp3.internal.platform.Platform log
failure connect timed out [Ljava.lang.StackTraceElement;@3850debc
INFO: <-- HTTP FAILED: java.net.SocketTimeoutException: connect timed out
Api Interface
interface ApiEndpoints {
@POST("/")
@Headers("Content-Type: application/json")
fun sendRequest(@Body json: PostModel): Call<ResponseModel>
}
Retrofit Client
class ApiClient {
val service = Retrofit.Builder()
.baseUrl("https://foo.foo.com:433")
.client(client) // Implement HttpLoggingInterceptor()
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiEndpoints::class.java)
}
Api Call
val test = ApiClient().service.sendRequest(
PostModel("web",
"ggrojeomieje2",
105,
"signal",
1,
"signal",
Params())
)
test.enqueue(object : Callback<ResponseModel> {
override fun onResponse(call: Call<ResponseModel>?, response: Response<ResponseModel>?) {
println("response " + response?.body()?.params?.timestamp.toString())
}
override fun onFailure(call: Call<ResponseModel>?, t: Throwable?) {
println("failure " + t?.message.toString())
}
})
Models
data class ResponseModel(val params: Params?)
data class PostModel(var transport: String? = null,
var userId: String? = null,
var moduleId: Int? = 0,
var moduleName: String? = null,
var moduleVersion: Int? = 0,
var operation: String? = null,
var params: Params?
)
data class Params(var timestamp: Unit? = null)
Response
{"params":{"timestamp":1.530346984546E9}}
This question appears to be off-topic. The users who voted to close gave this specific reason:
2 Answers
2
Try to set time out value in OkHttpClient
.
OkHttpClient
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
Also try changing request body types like x-www-form-urlencoded
or form-data
and header like this @Headers({"content-type: application/json"})
x-www-form-urlencoded
form-data
@Headers({"content-type: application/json"})
or
Check is there any network related issue or just try some simple GET
method too failing.
GET
Failed to connect to foo.foo.com/54.246.216.106:433
Check is there any network related issue or just try some simple GET method too failing.
– vbp
Jun 30 at 9:35
Stupid mistake, I had
https://foo.foo.com:433
instead of https://foo.foo.com:443
... Thank you for your time– thegrxp
Jun 30 at 9:53
https://foo.foo.com:433
https://foo.foo.com:443
Oh, I too didn't notice that.
– vbp
Jun 30 at 9:56
Stupid mistake, I had https://foo.foo.com:433 instead of https://foo.foo.com:443...
1 7jw4LoQGE0 Aoj h6ru5XzCeVEjmse62 OHfik1KVl,rWg1 6fSX8,MOfBcji yeEY3ue B7,jdzUIP8mFGgpnY,tdqKaTSMc
Just tried, now I get this error in onFailure():
Failed to connect to foo.foo.com/54.246.216.106:433
. Thanks– thegrxp
Jun 30 at 9:26