如何在 android 中使用 retrofit 2.3.0 将多张图片发送到服务器?

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

我尝试使用以下代码通过改造 2.3.0 将多张图像发送到服务器 但它对我不起作用:

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

并像这样将此多部分数据发送到服务器:

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

这是最终的改造界面:

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

我只需要通过改造 2.3.30 将多张图片发送到服务器,谁能建议我这样做 我收到错误:204(无内容)

不使用@Part,试试@Field

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

并在您的 Activity 中使用此方法发送 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();
    }
});

}