bad request while sending multiple images through

2019-08-02 01:54发布

问题:

I used to send multiple images to server through retrofit 2.3.0 , but I got bad request (400 err0r) my code:

  ArrayList<MultiPartImagesInputData> multiImagesList = new ArrayList<>();
    String[] multiplesImages = {"originalImgBlob", "img430Blog", "img200Blog", "img100Blog", "blurResponseBlob"};
    for (int i = 0; i < mFiles.size(); i++) {
        MultiPartImagesInputData multiPartImagesInputData = new MultiPartImagesInputData(multiplesImages[i], mFiles.get(i));
        multiImagesList.add(multiPartImagesInputData);
    }

and create multi part typed array like this:

 MultipartBody.Part[] multipartTypedOutputle = new MultipartBody.Part[mFiles.size()];

    for (int index = 0; index < mFiles.size(); index++) {
        Log.d("Upload request", "requestUploadSurvey: survey image " + index + "  " + mFiles.get(index));
        //  File file2 = new File(mResultBtmpProccess.get(index).path);
        RequestBody surveyBody = RequestBody.create(MediaType.parse("image/png"), mFiles.get(index));
        multipartTypedOutputle[index] = MultipartBody.Part.createFormData("imageFiles["+index+"]", multiImagesList.get(index).getFileName(), surveyBody);
    }

here is server call

  RequestBody memberId1 = RequestBody.create(MediaType.parse("text/plain"), Singleton.getInstance().getUserRegDetailsRespModel().getMId());
    RequestBody actionType1 = RequestBody.create(MediaType.parse("text/plain"), ACTION_TYPE_UPLOAD_NEW_PIC);
    Call<JsonObject> stringCall = mServerUtilities.getStringClassService(getApplicationContext(), "").postImages(memberId1, actionType1, multipartTypedOutputle);
    stringCall.enqueue(new retrofit2.Callback<JsonObject>() {


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

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


        }


        @Override
        public void onFailure(Call<JsonObject> call, Throwable t) {
            mUtilities.cancelProgressDialog();

            mUtilities.showAlert(t.getMessage(), getResources().getString(R.string.app_name));
            Log.d("onFail_fb_regist_res", t.getMessage());
        }
    });

interface:

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

what I did wrong in this ?

回答1:

Everything which you have written correctly, but error must be in API backend. Please re-check it. For me I checked in postman it is being uploaded even though the API doesn't allowing when it is being fired by mobile. They must allow form-data. :) I hope your issue would be solved.