在android中通过改进2.3.0发送多个图像时出现错误请求

时间:2018-01-08 12:26:39

标签: android retrofit2 android-image

我曾经通过改造2.3.0将多个图像发送到服务器, 但我的要求不好(400 err0r)  我的代码:

  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);
    }

并创建这样的多部分类型数组:

 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);
    }

这是服务器调用

  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());
        }
    });

接口:

                         @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);

我在这方面做错了什么?

1 个答案:

答案 0 :(得分:0)

您编写的所有内容都正确,但是API后端必须出错。请重新检查。对我来说,我签入了邮递员,尽管该API不允许在移动设备触发时上传,但它仍在上传。他们必须允许表单数据。 :)希望您的问题能得到解决。