带有原始类型数据的多部分表单数据以及要翻新的文件?

时间:2019-01-30 10:05:25

标签: android kotlin retrofit2 multipartform-data rx-java2

enter image description here

上述请求的值类型为String,Boolean,Int和使用多部分完成的图像附件。

 val retrofit = Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())

我还在请求中添加了标量转换器,而我的API是

@Multipart
@POST(AppConstants.Network.CREATE_STORE)
fun createStore(
   @Header("Authorization") token: String,

   //Step 1
   @Part imagePath: MultipartBody.Part?,
   @Part("Name") storeName: RequestBody,
   @Part("StoreOwnerName") storeOwnerName: RequestBody,
   @Part("StoreOwnerGender") storeOwnerGender: RequestBody,
   @Part("StoreOwnerDob") storeOwnerDob: RequestBody,
   @Part("AddressLine1") addressLine1: RequestBody,
   @Part("AddressLine2") addressLine2: RequestBody,
   @Part("AddressLine3") addressLine3: RequestBody,
   @Part("Place") place: RequestBody,
   @Part("PinCode") pinCode: Int,
   @Part("ContactNumber") contactNumber: RequestBody,
   @Part("Email") email: RequestBody,

   //Step 2
   @Part("IsRegistered") isRegistered: Boolean,
   @Part("HasGst") hasGst: Boolean,
   @Part("GSTNumber") GSTNumber: Int,
   @Part("HasInsurance") hasInsurance: Boolean,
   @Part("HasBankAccount") hasBankAccount: Boolean,

   //Step 3
   @Part("StaffSize") staffSize: Int,
   @Part("StoreSize") storeSize: Int,
   @Part("RunningFrom") runningFromDate: RequestBody,
   @Part("StoreTypeId") storeTypeId: Int,
   @Part("AverageTurnOver") averageTurnOver: Int,
   // @Part("AverageCustomerVisit") averageCustomerVisit: String,

   //  @Part("HasStaff") hasStaff: Boolean,
   @Part("OwnerResponseId") ownerResponseId: Int,
   @Part("Remarks") remarks: RequestBody

   ): Single<ResponseBody>

我的api定义是

   apiService.createStore(
            "bearer ".plus(AppPreferences(context!!).token!!),
            storeName = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            imagePath = MultipartBody.Part.createFormData(
                "ImagePath",
                store.imagePathFile?.name!!,
                RequestBody.create(MediaType.parse("image/*"), imageFilePath)
            ),
            storeOwnerName = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            storeOwnerGender = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            storeOwnerDob = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            addressLine1 = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            addressLine2 = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            addressLine3 = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            place = RequestBody.create(MediaType.parse("multipart/form-data"), "Fsafa"),
            pinCode = 642526,
            contactNumber = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            email = RequestBody.create(MediaType.parse("multipart/form-data"), "etet@gga.co"),
            isRegistered = false,
            hasGst = true,
            GSTNumber = 23423432,
            hasBankAccount = false,
            averageTurnOver = 345346,
            hasInsurance = false,
            staffSize = 546,
            ownerResponseId = 2,
            remarks = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            runningFromDate = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Name"),
            storeSize = 324,
            storeTypeId = 2
        )

1 个答案:

答案 0 :(得分:0)

首先,您需要添加Scalar Converter进行翻新,以便在后端正确获取您发送的数据。

Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())

要在表单数据POST请求中发送原始数据,您必须像这样发送它,

@Multipart
@POST(AppConstants.Network.CREATE_STORE)
fun createStore(
    @Header("Authorization") token: String,
    @Part imagePath: MultipartBody.Part?,
    @Part("Name") storeName: String,
    @Part("StoreOwnerName") storeOwnerName: String,
    @Part("StoreOwnerGender") storeOwnerGender: String?,
    @Part("IsRegistered") isRegistered: Boolean?,

调用请求的方式

apiService.createStore(
            token = "bearer ".plus(AppPreferences(context!!).token!!),
            imagePath = if (imageFilePath.isEmpty()) null else MultipartBody.Part.createFormData(
                "ImagePath",
                store.imagePathFile!!.name!!,
                RequestBody.create(MediaType.parse("image/*"), store.imagePathFile)
            storeName = store.name,
            storeOwnerName = store.storeOwnerName,
            storeOwnerGender = store.storeOwnerGender,
            isRegistered = store.isRegistered,