0

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.

Deep Shah
  • 1,334
  • 3
  • 20
  • 41
  • This is not a pro opinion but, I ran into this in some on my projects... Had to create separate requests on server-side for pictures and rest of my data. Also, usually I create a DataModel class for the body. Another approach, was to use `@Path("url_path")` and Multipart in body of the request. – Ionut J. Bejan Jun 05 '18 at 12:40
  • okay , I have just updated my question for REST api design & unfortunately I can't send body as parameter . I know it's bad design but I can't do anything about it. I'll check for @Path request. – Deep Shah Jun 05 '18 at 12:45
  • if you are fine with using okhtpp you can refer this [link](https://stackoverflow.com/questions/23512547/how-to-use-okhttp-to-upload-a-file) – Moulesh Jun 05 '18 at 13:15

1 Answers1

0

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.

Deep Shah
  • 1,334
  • 3
  • 20
  • 41