My application was made withMinha aplicação é feita com Kotlin, using Retrofit ande Gson, and I do several GET requests and they all works normally, but the post request I can't get it right.
The problem is that I can't get the response from the server. It doesn't execute anything after sending the requestTenho diversos gets para buscar informações e funcionam normalmente, but only in Post requests.manipulo os dados que retornam no OnResponse e ok, funciona normalmente. In my Get requests it works normally Porém quando executo como um POST isso não acontece, nunca chega no OnResponse.
I send the JSON body to my server O Json é enviado corretamente para o servidor, the server receives and returns codeque retorna status 201 with the saved registerde criado, but my Retrofit Call does not execute onResponsemas mesmo assim o código do OnResponse nunca é executado, so I can't know when the request got done or notportanto não consigo controlar quando realmente deu certo o POST.
I don't know what I'm doing wrong, could anyone help me, pleaseO que será que está errado aqui?
So here's my initializerInitializer:
private val retrofit = Retrofit.Builder()
.baseUrl("http://myserverurl.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()
fun orderService () = retrofit.create(OrderService ::class.java)
Here's OrderService.kt
interface OrderService {
@POST("pedido")
fun insert(@Body order: OrderEntity, @Header("mykey") myKey:String) : Call<OrderEntity>
}
Here's my callChamada:
val call = RetrofitInitializer().orderService().insert(order, mMyKey)
call.enqueue(object : Callback<OrderEntity> {
override fun onResponse(call: Call<OrderEntity>?,
response: Response<OrderEntity>?) {
response?.body()?.let {
Toast.makeText(mContext, "It works!", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<OrderEntity>?, t: Throwable?) {
Log.e("onFailure error", t?.message)
}
})
TheLembrando que o request worksfunciona, but never executesó não retorna pro onResponse or onFailure function.