Kotlin 如何使用改装2发送包含图像的其他对象列表的对象
我需要有人帮我处理这个案子: 下面是我的对象的格式:Kotlin 如何使用改装2发送包含图像的其他对象列表的对象,kotlin,retrofit2,rx-java2,okhttp,Kotlin,Retrofit2,Rx Java2,Okhttp,我需要有人帮我处理这个案子: 下面是我的对象的格式: { "transport": true, "fraisTransport": "string" "cars": [ { "id": "string", "prix": "string", "photo_url1" : "string", "photo_url2" : "string" }, { "id": "string", "prix":
{
"transport": true,
"fraisTransport": "string"
"cars": [
{
"id": "string",
"prix": "string",
"photo_url1" : "string",
"photo_url2" : "string"
},
{
"id": "string",
"prix": "string",
"photo_url1" : "string",
"photo_url2" : "string"
}
]
}
这是我的Api接口
@Multipart
@POST("declaration")
fun addDeclaration( @Part carsImage: Array<MultipartBody.Part> ,
@Part propertyCars: MultipartBody.Part,
@Part dataDeclaration: RequestBody): Observable<Response>
现在我的问题是如何为cars对象设置变量。欢迎提出任何建议。谢谢我建议您按照以下方法以类的形式发送数据:
或者,您可以创建json并按如下方式发送:经过长时间的搜索,我找到了解决问题的方法。也许这能帮助一些人
@Multipart
@POST("declaration")
fun addDeclaration(@Part carsImage: MutableList<MultipartBody.Part>, @PartMap
declaration: HashMap<String?, RequestBody?>?): Observable<Response>
val declarationInfo: HashMap<String?, RequestBody?>? = HashMap()
declarationInfo["transport"] = createPartFromString("" + declarationVente.transport)
task["fraisTransport"] = createPartFromString("" +declarationVente.fraisTransport)
val body: MutableList<MultipartBody.Part> = mutableListOf()
val listA: List<Cars> = mutableListOf<Cars>().apply {
for (aD in 0 until it.size) {
declarationInfo["cars[$aD][id]"] = createPartFromString(it[aD].animalId.toString())
declarationInfo["cars[$aD][prix]"] = createPartFromString(it[aD].prixDeclaration.toString())
val photoFile1 = File(it[aD].photo1)
val reqFile = photoFile1.asRequestBody("image/*".toMediaTypeOrNull())
val imageFile1 = MultipartBody.Part.createFormData("cars[$aD][uploadFile1]", photoFile1.name, reqFile)
body.add(imageFile1)
}
}
@Multipart
@邮政(“申报”)
fun addDeclaration(@Part carsImage:MutableList,@PartMap
声明:HashMap?:可观察
val声明信息:哈希映射?=HashMap()
declarationInfo[“transport”]=createPartFromString(“+declarationVente.transport”)
任务[“fraisTransport”]=createPartFromString(“+DeclarationVent.fraisTransport”)
val body:MutableList=mutableListOf()
val listA:List=mutableListOf().apply{
用于(在0中添加广告,直到达到.size){
declarationInfo[“cars[$aD][id]”]=createPartFromString(it[aD].animalId.toString())
declarationInfo[“cars[$aD][prix]”]=createPartFromString(it[aD].prixDeclaration.toString())
val photoFile1=文件(它[aD].photo1)
val reqFile=photoFile1.asRequestBody(“image/*”.tomediateTypeOrnull())
val imageFile1=MultipartBody.Part.createFormData(“cars[$aD][uploadFile1]”,photoFile1.name,reqFile)
body.add(imageFile1)
}
}
谢谢你的建议。我已经看过这首短裙了。在我的例子中,photo_url1是设备上的图像文件。我应该使用MultipartBody。部分原因。在对象中,汽车是阵列列表,但在许多图图中,仅使用单个对象和一个图像
@Multipart
@POST("declaration")
fun addDeclaration(@Part carsImage: MutableList<MultipartBody.Part>, @PartMap
declaration: HashMap<String?, RequestBody?>?): Observable<Response>
val declarationInfo: HashMap<String?, RequestBody?>? = HashMap()
declarationInfo["transport"] = createPartFromString("" + declarationVente.transport)
task["fraisTransport"] = createPartFromString("" +declarationVente.fraisTransport)
val body: MutableList<MultipartBody.Part> = mutableListOf()
val listA: List<Cars> = mutableListOf<Cars>().apply {
for (aD in 0 until it.size) {
declarationInfo["cars[$aD][id]"] = createPartFromString(it[aD].animalId.toString())
declarationInfo["cars[$aD][prix]"] = createPartFromString(it[aD].prixDeclaration.toString())
val photoFile1 = File(it[aD].photo1)
val reqFile = photoFile1.asRequestBody("image/*".toMediaTypeOrNull())
val imageFile1 = MultipartBody.Part.createFormData("cars[$aD][uploadFile1]", photoFile1.name, reqFile)
body.add(imageFile1)
}
}