代码之家  ›  专栏  ›  技术社区  ›  uma

如何在android中使用改型2.3.0向服务器发送多个图像?

  •  1
  • uma  · 技术社区  · 7 年前

    我尝试了以下代码,通过改装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 回复  |  直到 4 年前
        1
  •  1
  •   Anurag Shrivastava    7 年前

    而不是使用@Part try@字段

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