Uploading a single image does not seem to pose a problem with modification 2.
However, I cannot figure out how to upload 2 images at a time.
if you follow the documentation: http://square.imtqy.com/retrofit/2.x/retrofit/retrofit2/http/PartMap.html
File file = new File(path, "theimage"); File file2 = new File(path2, "theimage"); RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file); RequestBody requestBody2 = RequestBody.create(MediaType.parse("image/png"), file2); Map<String, RequestBody> params = new HashMap<>(); params.put("image2", requestBody2 ); Call<ResponseBody> call = service.upload(requestBody, params); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Response<ResponseBody> response, Retrofit retrofit) { Log.v("Upload", "success"); }
interface:
public interface FileUploadService { @Multipart @POST("/upload") Call<ResponseBody> upload( //@Part("image_logo\"; filename=\"image.png\" ") RequestBody file, @Part("file") RequestBody file, @PartMap Map<String, RequestBody> params // @Part("description") String description );
this gives "Upload: success", but on the server side I get gibberish:
CONTENT_TYPE: multipart / form-data; border = 50fbfeb3-3abc-4f15-b130-cdcb7e3a0e4f
CONTENTS CONTENTS: Array ([file] => PNG IHDR L alotofbinarygibberish ....... snip [file2] => PNG IHDR L is more than binary gibberish ...
Can someone point me in the right direction?
single upload works, so the problem is not that I'm trying to upload 2 or more images.
if I change it to this:
HashMap<String, RequestBody> partMap = new HashMap<String, RequestBody>(); partMap.put("file\"; filename=\"" + file.getName(), requestBody); partMap.put("file\"; filename=\"" + file2.getName(), requestBody); Call<ResponseBody> call = service.upload(partMap);
@Multipart @POST("/upload") Call<ResponseBody> upload( @PartMap() Map<String, RequestBody> partMap,
I don't get any gibberish, but only the second image is uploaded ...!?
UPDATE
I tried this Retrofit (2.0 beta2) Downloading a multi-page file does not work , but I get an error message that @body cannot use with multipart: Java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. (parameter # 1)
for (String key : keys) { Bitmap bm = selectedImages.get(key); File f = new File(saveToInternalStorage(bm, key), key); if (f.exists()) { buildernew.addFormDataPart(key, key + ".png", RequestBody.create(MEDIA_TYPE, f)); } } RequestBody requestBody = buildernew.build();
-
Call<ResponseBody> upload( @Body RequestBody requestBody