如何使用改造2发送带有图像的另一个对象列表的对象

问题描述 投票:0回答:2

在这种情况下,我需要有人帮助:这里是我对象的格式:

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

这里我如何为dataDEclaration设置变量

   val jsonObject = JSONObject()
       jsonObject.put("transport", declaration.transport)
       jsonObject.put("frais_transport", declaration.fraisTransport)
       val bodyDeclarationInfo = jsonObject.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())

现在我的问题是如何为汽车对象设置变量。请任何建议将受到欢迎。谢谢

kotlin retrofit2 rx-java2 okhttp
2个回答
0
投票

我让您按照该方法将数据作为类发送:https://futurestud.io/tutorials/retrofit-send-objects-in-request-body

或者您可以创建您的json并像这样发送它:How to POST raw whole JSON in the body of a Retrofit request?


0
投票

经过长时间的搜索,我找到了解决问题的方法。也许那可以帮助别人。

 @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)
                     }
                }
© www.soinside.com 2019 - 2024. All rights reserved.