如何通过在android中改进2.3.0一次向服务器发送多个图像

时间:2018-01-08 08:38:31

标签: android server retrofit send android-image

我尝试下面的代码通过改造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(没有内容)

1 个答案:

答案 0 :(得分:0)

而不是使用@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 中使用此方法发送帖子

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

}