Retrofit: Add the Row List option to the Multipage Query

I am trying to add a string list parameter to a query with multiple parts.

Using Apache Http, I set the parameter as follows:

MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(mpEntity); for (User member : members) { mpEntity.addPart("user_ids[]", new StringBody(member.id.toString())); } 

How to do this when retrofitting?

+6
source share
1 answer

Take a look at MultipartTypedOutput . I don’t think that there is built-in support, so you have to build it yourself or write Convertor .

In your service interface:

 @POST("/url") Response uploadUserIds(@Body MultipartTypedOutput listMultipartOutput); 

Your subscriber:

 MultipartTypedOutput mto = new MultipartTypedOutput(); for (String userId : userIds){ mto.addPart("user_ids[]", new TypedString(userId)); } service.uploadUserIds(mto); 

This will build a similar result:

  Content-Type: multipart/form-data; boundary=49f201f1-7379-4390-9ea5-97d74e78bb63 Content-Length: 820 --49f201f1-7379-4390-9ea5-97d74e78bb63 Content-Disposition: form-data; name="name" Content-Type: text/plain; charset=UTF-8 Content-Length: 10 Content-Transfer-Encoding: binary yourString --49f201f1-7379-4390-9ea5-97d74e78bb63 Content-Disposition: form-data; name="name" Content-Type: text/plain; charset=UTF-8 Content-Length: 10 Content-Transfer-Encoding: binary yourString --49f201f1-7379-4390-9ea5-97d74e78bb63 Content-Disposition: form-data; name="name" Content-Type: text/plain; charset=UTF-8 Content-Length: 10 Content-Transfer-Encoding: binary yourString --49f201f1-7379-4390-9ea5-97d74e78bb63 Content-Disposition: form-data; name="name" Content-Type: text/plain; charset=UTF-8 Content-Length: 10 Content-Transfer-Encoding: binary yourString --49f201f1-7379-4390-9ea5-97d74e78bb63-- 
+2
source

All Articles