简体   繁体   中英

Android Kotlin Retrofit Post Request Inputted data does not sent

So I have to handle a POST request with its body data taken from some input in a form. The Endpoint of this service is https://someUrl.com/switching-product/switch?orderID=A002&procode=0200011&nip=P19120 The Response returned from Postman is like this .

The body of this request is like this:

在此处输入图像描述

In this case, I have this Interface for handling a POST request:

///Endpoint: https://someUrl.com/switching-product/switch?orderID=A002&procode=0200011&nip=P19120

 interface editProductDetail{
    @FormUrlEncoded
    @POST("switch")
    fun editProductDetail(@Query("orderID") orderID: String,
                      @Query("procode") procode: String,
                      @Query("nip") nip : String,

                      @Field("procode_new") procodeNew: String,
                      @Field("item_qty_new") itemQtyNew: String,
                      @Field("item_price_new") itemPriceNew: String,
                      @Field("item_name_new") itemNameNew: String,
                      @Field("total_price_new") totalPriceNew: String): Call<OutstandingOrderDetailPOJODataClassDetailItem>
}

This is the data class I use:

 data class OutstandingOrderDetailPOJODataClassDetailItem(

    @field:SerializedName("item_price_new")
    val itemPriceNew: Int? = null,

    @field:SerializedName("item_name_new")
    val itemNameNew: String? = null,

    @field:SerializedName("total_price")
    val totalPrice: Int? = null,

    @field:SerializedName("item_price")
    val itemPrice: Int? = null,

    @field:SerializedName("item_name")
    val itemName: String? = null,

    @field:SerializedName("status_refund")
    val statusRefund: String? = null,

    @field:SerializedName("detail_id")
    val detailId: Int? = null,

    @field:SerializedName("procode_new")
    val procodeNew: String? = null,

    @field:SerializedName("refund_date")
    val refundDate: String? = null,

    @field:SerializedName("request_refund")
    val requestRefund: String? = null,

    @field:SerializedName("procode")
    val procode: String? = null,

    @field:SerializedName("last_update")
    val lastUpdate: String? = null,

    @field:SerializedName("item_qty_new")
    val itemQtyNew: Int? = null,

    @field:SerializedName("order_id")
    val orderId: String? = null,

    @field:SerializedName("total_price_new")
    val totalPriceNew: Int? = null,

    @field:SerializedName("item_qty")
    val itemQty: Int? = null,

    @field:SerializedName("refund")
    val refund: Int? = null
)

As you can see above, the body has attributes that are included in the data class as well. (This data class is also used in a related service in the App that uses the inputted data of this service).

And this is the function to trigger the POST request:

NetworkConfig().editOutstandingOrderProductDetailService().editProductDetail(
        selectedOrderId,
        selectedProcode,
        selectedNip,
        procodeNew,
        itemNewQty,
        itemPriceNew,
        itemNewName,
        totalPriceNew

    ).enqueue(object :
        Callback<OutstandingOrderDetailPOJODataClassDetailItem> {
        override fun onFailure(call: Call<OutstandingOrderDetailPOJODataClassDetailItem>, t: Throwable) {
            Log.i("Order", "It Failed!!")

            if (call.isCanceled) {
                Toast.makeText((activity as AppCompatActivity), "Request Aborted", Toast.LENGTH_SHORT).show()

            } else {
                Toast.makeText((activity as AppCompatActivity), t.localizedMessage, Toast.LENGTH_SHORT).show()
            }
        }

        override fun onResponse(
            call: Call<OutstandingOrderDetailPOJODataClassDetailItem>,
            response: Response<OutstandingOrderDetailPOJODataClassDetailItem>
        ) {
            Log.i("Order", "Switching Process done!!!")
            Log.i("Order", "Status: ${response.body()}")
        }
    })

From above, it prints the response in the logCat like this:

在此处输入图像描述

Am I missing something here? Or there's something I need to Change? If there's any detail that I missed to point out, just let me know!

Your request is a JSON object , not a formurl . @Field tag is used when you want to pass your parameters as formurl

Use model class or JsonObject with @Body tag to pass parameters as JsonObject .

Model class Exmaple,

data class TestClass{  
    val text1: String,
    val text2: String 
}

Now Pass this class as request

@POST("URL")
fun apiName(@Body request: TestClass);

JSON Object Exmaple,

JSONObject paramObject = new JSONObject();
 paramObject.put("key1", "value1");
 paramObject.put("key1", "vaalue2");

Now pass it as String or JsonObject

@POST("URL")
fun apiName(@Body request: String); // pass as String

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM