Retrofit Uploading multiple images to a single key

前端 未结 4 708
孤独总比滥情好
孤独总比滥情好 2020-11-28 05:10

I am using Retrofit to upload images to my server. Here I need to upload multiple images for a single key. I have tried with Postman web client it is working well. Here is a

4条回答
  •  [愿得一人]
    2020-11-28 05:33

    We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution
    WebServicesAPI

    @Multipart
    @POST(WebServices.UPLOAD_SURVEY)
    Call uploadSurvey(@Part MultipartBody.Part[] surveyImage,
                                                 @Part MultipartBody.Part propertyImage,
                                                 @Part("DRA") RequestBody dra);
    

    Here is the method for uploading the files.

    private void requestUploadSurvey () {
        File propertyImageFile = new File(surveyModel.getPropertyImagePath());
        RequestBody propertyImage = RequestBody.create(MediaType.parse("image/*"),
                                                       propertyImageFile);
        MultipartBody.Part propertyImagePart = MultipartBody.Part.createFormData("PropertyImage",
                                                                                 propertyImageFile.getName(),
                                                                                 propertyImage);
    
        MultipartBody.Part[] surveyImagesParts = new MultipartBody.Part[surveyModel.getPicturesList()
                                                                                   .size()];
    
        for (int index = 0; index <
                            surveyModel.getPicturesList()
                                       .size(); index++) {
            Log.d(TAG,
                  "requestUploadSurvey: survey image " +
                  index +
                  "  " +
                  surveyModel.getPicturesList()
                             .get(index)
                             .getImagePath());
            File file = new File(surveyModel.getPicturesList()
                                            .get(index)
                                            .getImagePath());
            RequestBody surveyBody = RequestBody.create(MediaType.parse("image/*"),
                                                        file);
            surveyImagesParts[index] = MultipartBody.Part.createFormData("SurveyImage",
                                                                         file.getName(),
                                                                         surveyBody);
        }
    
        final WebServicesAPI webServicesAPI = RetrofitManager.getInstance()
                                                             .getRetrofit()
                                                             .create(WebServicesAPI.class);
        Call surveyResponse = null;
        if (surveyImagesParts != null) {
            surveyResponse = webServicesAPI.uploadSurvey(surveyImagesParts,
                                                         propertyImagePart,
                                                         draBody);
        }
        surveyResponse.enqueue(this);
    }
    

提交回复
热议问题