【问题标题】:bad request while sending multiple images through retrofit 2.3.0 in android通过android中的retrofit 2.3.0发送多个图像时出现错误请求
【发布时间】:2018-08-18 01:55:26
【问题描述】:

我曾经通过改造 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);

我做错了什么?

【问题讨论】:

    标签: android retrofit2 android-image


    【解决方案1】:

    你写的一切都正确,但错误必须在 API 后端。请重新检查。对我来说,我在邮递员中检查了它正在被上传,即使当它被移动设备触发时 API 不允许。他们必须允许表单数据。 :) 我希望你的问题能得到解决。

    【讨论】:

      猜你喜欢
      • 2014-12-29
      • 2021-08-29
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2019-04-19
      相关资源
      最近更新 更多