POST using Retrofit is not returning to the Onresponse function

Asked

Viewed 320 times

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

  • 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"

2 answers

0

I noticed something in its interface, in version 2 of the retrofit the data encoded by form are sent when @FormUrlEncoded. Each key value pair is annotated with @True of the name and the object providing the value. Make in the following pattern:

@FormUrlEncoded
@POST("Tokencelular")
Call<TokenModel> gravaToken(@Field("tokenDevice") String tokenDevice);

In your GET methods you don’t need.

To learn more, visit the documentation: https://square.github.io/retrofit/ and go to topic FORM ENCODED AND MULTIPART

Hugs!

  • I’ll test and get back to you, thank you very much!

  • I made some changes based on that, but it didn’t work. If that were the case, the request wouldn’t even be sent and received by the server, right? In this case it sends correctly but I can’t get the return. I tried to use Field instead of Body, is that right? From what I understood this Formurlencoded is necessary to send beyond the form data node, also send a node with an authentication key for example inside my JSON? In my case I put the key in Header. In Logcat nothing appeared, nor Failure

0


I managed to solve it. Actually, the problem was quite another. Debugging better I realized that I was returning to Onresponse but with code 500, and then I discovered that one of the fields was giving a problem in the API that did not appear in my tests in Postman. The error was happening in the API’s Answer that was not converting a date value correctly, leading to 500 status even though it saved the record correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.