Search code examples
androidretrofit2image-uploadingmultipartform-dataurl-encoding

How to send FormData with MultiPart request using retrofit2?


I'm developing an android app where I need to upload multiple images to php server from android app & currently I'm stuck with few questions which are as follow :

1) Can we pass field parameters along with images to upload as Multipart request & if so what should be the correct request format ?

2 ) Is below one is the correct way to upload multiple images from Android ?

3 ) What should be the correct strategy to upload multiple images smoothly?

Here is my tentative code which I think helps anyone to understand what I'm trying to achieve .

@Multipart
@FormUrlEncoded
@POST(AppConstants.BASE_URL+AppConstants.POST_AD)
Observable<Response<PostedAd>> postAd(@Header(AppConstants.HEADER_ACCESS_TOKEN) String headerToken,@Part("images[]") RequestBody files, @Field(AppConstants.FIELD_AD_TITLE) String adTitle, @Field(AppConstants.FIELD_AD_DESCRIPTION) String adDesc,@Field(AppConstants.FIELD_AD_ADDRESS) String adAddress,@Field(AppConstants.FIELD_AD_CITY) String city,@Field(AppConstants.FIELD_AD_STATE) String state,@Field(AppConstants.FIELD_AD_ZIPCODE) String zipcode,@Field(AppConstants.FIELD_AD_CONTACT_NUMBER) String contactNumber,@Field(AppConstants.FIELD_AD_TYPE) String adType,@Field(AppConstants.FIELD_AD_BUDGET_RATE) String adbudgetOrRate);

Note : REST APIs are designed in such way that it doesn't allow me to send all fields as Single Body parameter.


Solution

  • I got the solution to my problem . Basically my problem was to send form data with multiple images & it's present in Retrofit2 .

    So , anyone looking for the same kind of requirement then here are links which might help you :

    Send Request Parameters with MultiPart Request using PartMap
    Upload multiple images using Retrofit2

    Combine both of these solution together & you can send request parameters along with multiple files.