Skip to main content
2 de 5
adicionou 68 caracteres ao conteúdo

Retrofit POST request never returns to OnResponse function

My application was made with Kotlin, using Retrofit and 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 request, but only in Post requests.. In my Get requests it works normally.

I send the JSON body to my server, the server receives and returns code 201 with the saved register, but my Retrofit Call does not execute onResponse, so I can't know when the request got done or not.

I don't know what I'm doing wrong, could anyone help me, please?

So here's my initializer:

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("acesskey") acessKey:String) : Call<PedidoEntity>
}

Here's my call:

val call = RetrofitInitializer().orderService().insert(order, mAcessKey)
    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)
        }
    })

The request works, but never execute onResponse or onFailure function.