2

I need somebody help for this case: Here the format of my object :

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

Here is my Api interface

@Multipart
@POST("declaration")
fun addDeclaration( @Part carsImage: Array<MultipartBody.Part> ,
                    @Part propertyCars: MultipartBody.Part,
                    @Part dataDeclaration:  RequestBody): Observable<Response>

here how i set the variable for 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())

Now my problem is how to set the variable for cars objects. Please any suggestions will welcome. Thanks

K. Donon
  • 254
  • 2
  • 13

2 Answers2

1

After long search i got a way to solve my problem. Perhaps that can help somebody.

 @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)
                     }
                }
K. Donon
  • 254
  • 2
  • 13
0

I adivise you to either send data as a class following this methode : https://futurestud.io/tutorials/retrofit-send-objects-in-request-body

Or you can create your json and send it like this : How to POST raw whole JSON in the body of a Retrofit request?

XCarb
  • 735
  • 10
  • 31
  • Thanks for the advise. I've already seen this tuto. In my case *photo_url1* are image File on the device. and i should use MultipartBody.Part for that. in the object cars are arraylist but in many tuto the use only single object with one image – K. Donon Nov 27 '19 at 14:13