0
My application is made with Kotlin Retrofit and Gson.
I have several gets to fetch information and work normally, manipulate the data returning in Onresponse and ok, works normally. But when I run as a POST this doesn’t happen, it never arrives in Onresponse. Json is sent correctly to the server, which returns 201 created status, but even then the Onresponse code is never executed, so I can’t control when the POST actually worked.
I wonder what’s wrong here?
Initializer:
private val retrofit = Retrofit.Builder()
.baseUrl("http://myserverurl.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()
fun orderService () = retrofit.create(OrderService ::class.java)
Orderservice.kt
interface OrderService {
@POST("pedido")
fun insert(@Body order: OrderEntity, @Header("mykey") myKey:String) : Call<OrderEntity>
}
Calling for:
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)
}
})
Remembering that the request works, only does not return pro onResponse.
opa, blza!? has already put a debug on onResponse and onFailure? may be giving error 500 and falling into failure. Fault house you can catch what’s going on. Have you tried Postman? It’s all right? it makes it easier to test your endpoints
– Leonardo Figueiredo
Eai, blz and vc? Then, in Postman returns a json of the data I sent and status 201 that was successfully created (really was). I tried to debug all the lines basically within the enqueue but it seems never "get there"
– Thiago Rocha