Search code examples
androidserverretrofitsendandroid-image

How to send multiple images to server using retrofit 2.3.0 in android?


I tried below code to send multiple images to server through retrofit 2.3.0 but its not worked to me :

 Map<String, RequestBody> multiPartMap = new HashMap<>();

    multiPartMap.put("originalImgBlob",RequestBody.create(MediaType.parse("image/png"),  mFiles.get(0)));
    multiPartMap.put("img430Blog",RequestBody.create(MediaType.parse("image/png;base64"),  mFiles.get(1)));
    multiPartMap.put("img200Blog",RequestBody.create(MediaType.parse("image/png;base64"),  mFiles.get(2)));
    multiPartMap.put("img100Blog",RequestBody.create(MediaType.parse("image/png;base64"),  mFiles.get(3)));
    multiPartMap.put("blurResponseBlob",RequestBody.create(MediaType.parse("image/png;base64"),  mFiles.get(4)));

and send this multipart data to server like this :

 Call<JsonObject> stringCall = mServerUtilities.getStringClassService(getApplicationContext(), "").postImage(Singleton.getInstance().getUserRegDetailsRespModel().getMId(), ACTION_TYPE_UPLOAD_NEW_PIC,multiPartMap);


        stringCall.enqueue(new retrofit2.Callback<JsonObject>() {


            @Override
            public void onResponse(Call<JsonObject> call, @NonNull retrofit2.Response<JsonObject> response) {

                Log.d("fb_regist_response", "--->" + "" + response);


            }


            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                mUtilities.showAlert(t.getMessage(), getResources().getString(R.string.app_name));
                Log.d("onFail_fb_regist_res", t.getMessage());
            }
        });

and this is the final retrofit interface:

    @Multipart
@POST("/api/mbrphotos/prfImgIU/{memberId}/{actionType}")
Call<JsonObject>  postImage(@Path("memberId") String memberId,
                            @Path("actionType") String actionType,
                            @PartMap Map<String, RequestBody> multipartTypedOutput);

I need to send multiple images to server through retrofit 2.3.30 only ,can anyone suggest me to do that I got error: 204(no content )


Solution

  • Instead of using @Part try @Field

    @Multipart
        @POST("/api/mbrphotos/prfImgIU/{memberId}/{actionType}")
        Call<JsonObject> postImage(@Part("memberId") RequestBody memberId,
                                   @Part("actionType") RequestBody actionType,
                                   @Part MultipartBody.Part[] multipartTypedOutput);
    

    and in your Activity use this method to send the post

    public void sendPost(String memberId, String actionType) {
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("Please wait...");
    dialog.setCancelable(false);
    dialog.show();
    
    MultipartBody.Part[] multipartTypedOutput = new MultipartBody.Part[imageModelArrayList.size()];
    
    for (int index = 0; index < imageModelArrayList.size(); index++) {
        Log.d("Upload request", "requestUploadSurvey: survey image " + index + "  " + imageModelArrayList.get(index).path);
        File file2 = new File(imageModelArrayList.get(index).path);
        RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"), file2);
        multipartTypedOutput[index] = MultipartBody.Part.createFormData("imageFiles[]", file2.getPath(), surveyBody);
    }
    
    RequestBody memberId1 = RequestBody.create(MediaType.parse("text/plain"), memberId);
    RequestBody actionType1 = RequestBody.create(MediaType.parse("text/plain"), actionType);
    
    apiService.postImage(memberId1, actionType1, multipartTypedOutput).enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            Log.d("fb_regist_response", "--->" + "" + response);
            dialog.dismiss();
        }
    
        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            Log.d("onFail_fb_regist_res", t.getMessage());
            dialog.dismiss();
        }
    });
    
    }