Skip to main content
Pergunta fechada como "Não é adequada para este site" por Largato
Pergunta reaberta por João Martins, LeAndrade, Renan Gomes, Erlon Charles, Leandro Angelo
título editado
Link

Retrofit POST request never returns tousando Retrofit não está retornando para a função OnResponse function

removeu 136 caracteres do conteúdo
Fonte Link

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.

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("mykey") myKey:String) : Call<OrderEntity>
}

Here's my call:

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)
        }
    })

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

Minha aplicação é feita com Kotlin Retrofit e Gson.

Tenho diversos gets para buscar informações e funcionam normalmente, manipulo os dados que retornam no OnResponse e ok, funciona normalmente. Porém quando executo como um POST isso não acontece, nunca chega no OnResponse. O Json é enviado corretamente para o servidor, que retorna status 201 de criado, mas mesmo assim o código do OnResponse nunca é executado, portanto não consigo controlar quando realmente deu certo o POST.

O que será que está errado aqui?

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>
}

Chamada:

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)
        }
    })

Lembrando que o request funciona, só não retorna pro onResponse.

Pergunta fechada como "Não é adequada para este site" por Barbetta, viana, rLinhares, hkotsubo, Roberto de Campos
removeu 10 caracteres do conteúdo
Fonte Link

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""mykey") acessKeymyKey:String) : Call<PedidoEntity>Call<OrderEntity>
}

Here's my call:

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

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.

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("mykey") myKey:String) : Call<OrderEntity>
}

Here's my call:

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)
        }
    })

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

adicionou 68 caracteres ao conteúdo
Fonte Link
Carregando
Fonte Link
Carregando