android - Retrofit POST with empty array
问题描述
I need to send a POST with Retrofit:
Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(
GsonConverterFactory.create(
GsonBuilder().serializeNulls().create()
))
.client(okHttpClient)
.build()
@POST(endpoint)
@FormUrlEncoded
fun partialPickupsDelivery(
@HeaderMap headers: Map<String, String>,
@Field("userId") userId: String,
@Field("purchaseIds[]") purchases: ArrayList<String>
): Call<ResponseBody>
The server is waiting something like this:
purchaseIds=["1234", "2345", "3456"]
The problem comes when I try to send an empty array (not null) because the server is waiting:
purchaseIds=[]
But I'm sending only the first param userId
(I think retrofit is removing the second one because the array is empty).
Is there any way to send purchaseIds=[]
?
Thx
解决方案
我终于找到了一个适合我的解决方案:
@POST(endpoint)
fun partialPickupsDelivery(
@HeaderMap headers: Map<String, String>,
@Body params: HashMap<String, Any>
): Call<ResponseBody?>
哪里params
是:
val params = HashMap<String, Any>().apply {
this["purchaseIds"] = destinationsId
this["userId"] = deliverId
}
它是这样称呼的:
val call: Call<ResponseBody> =
service.partialPickupsDelivery(
headers,
params
)
我希望这可以帮助某人
推荐阅读
- r - 使用基础 R 从 dplyr::mutate_at() 复制结果
- node.js - 将 Postgres 字段转换为变量 nodejs 的问题
- sql - SQL 循环,条件查询
- html - 在不加载的情况下创建多页网站
- python - 为键创建字典:二维列表中的值
- android - 用android studio实现超类方法的位置
- mysql - 无法使用 mysql 工作台连接到 heroku mysql 数据库 clearDB
- node.js - Angular 6 SSH2 - 如何使用 Promise 处理错误?
- facebook - 各种 Facebook API 页面“instagram”字段之间有什么区别
- python - 如何将python输出发送到电报频道而不是组和gmail电子邮件组