Android Kotlin Retrofit Post 请求输入的数据未发送

标签 android kotlin retrofit

因此,我必须处理 POST 请求,其正文数据取自表单中的某些输入。 此服务的端点是 https://someUrl.com/switching-product/switch?orderID=A002&procode=0200011&nip=P19120 从 postman 返回的响应类似于 this

该请求的正文如下:

enter image description here

在本例中,我有一个用于处理 POST 请求的接口(interface):

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

这是我使用的数据类:

 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
)

正如您在上面所看到的,主体也具有数据类中包含的属性。 (该数据类也用于使用该服务的输入数据的App中的相关服务)。

这是触发POST请求的函数:

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

从上面开始,它在 logCat 中打印响应,如下所示:

enter image description here

我在这里遗漏了什么吗?或者我需要改变什么?如果我遗漏了任何细节,请告诉我!

最佳答案

您的请求是一个 JSON 对象,而不是 formurl。 当您想要将参数作为 formurl

传递时,使用 @Field 标签

使用模型类JsonObject与@Body标记将参数作为JsonObject传递。

模型类Exmaple,

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

现在将此类作为请求传递

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

JSON 对象示例,

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

现在将其作为 StringJsonObject 传递

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

关于Android Kotlin Retrofit Post 请求输入的数据未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62295196/

相关文章:

kotlin - 如果构造函数不能返回 null,为什么我需要在初始化后进行类型检查?

android - Kotlin 中的 Gson toJson 和 ArrayList

android - 使用 Retrofit 填充阵列适配器

android - 如何在 Retrofit 2 中使用 Google Agera

android - 实现activity如何在android中使用jar中的方法

Android:访问构成视频的图像

android - Android Studio Chipmunk 中缺少数据库检查器选择

spring-boot - 如何让 Spring Boot Kotlin bootRun 和 devtools 重启得更快?

java - 带位图的高效onDraw和硬件加速的复杂裁剪

Android ORMLite - ForeignCollection 子项具有空的外部字段