使用multipart.Part发送多个文件

时间:2019-07-07 06:08:20

标签: android email retrofit2 multipart

我正在尝试用gmail发送多个文件(图像),但我无法发送它。我尝试将多部分放入数组中,但是没有传递单个邮件中的文件,而是传递了两个邮件。我的代码如下:

接口:

library(splines)
svglm(fpl ~ ns(age,4) + gender, design = nhanesDesign)

主要活动:

public interface EmailService {
@Multipart
@POST("/send/email")
Call<OnlineAushadiModel> sendEmailOnlineAushadi(
                                  @Part("FROM") RequestBody requestFrom,
                                  @Part("TO") RequestBody requestTo,
                                  @Part("SUBJECT") RequestBody requestSubject,
                                  @Part("MailContain") RequestBody requestMailContain,
                                  @Part("FileName") RequestBody requestFileName,
 }

我尝试了其他方法,例如将api调用置于循环之外,但仍然没有帮助。有人可以帮我在Multipart中发送多个文件吗?

1 个答案:

答案 0 :(得分:0)

您不应在循环中上传文件。像代码示例一样!

 private void CallAPI() {

    boolean HaveFile;
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setMessage("Uploading...");
    pd.setCancelable(false);
    pd.show();

    ArrayList<MultipartBody.Part> body = new ArrayList<>();
     //image path is a string list of seleted files path
    if (imagePath.size() != 0) {
        for (int i = 0; i < imagePath.size(); i++) {
            //creating a file
            File file = new File(imagePath.get(i));
            //creating request body for file
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            body.add(MultipartBody.Part.createFormData("uploaded_file", file.getName(), requestFile));


        }
        HaveFile=true;
    } else {
        RequestBody attachmentEmpty = RequestBody.create(MediaType.parse(/*"multipart/form-data"*/"text/plain"), "");

        body.add( MultipartBody.Part.createFormData(/*"uploaded_file"*/"attachment", "0736E389-EF21-4286-BEBF-14CCD48B04A6", attachmentEmpty));
        HaveFile=false;
    }




    APIService service =
            ServiceGenerator.getclient().create(APIService.class);

    Call<String> call = service.uploadMulFiles(body.size() == 0 ? null : body ,to,from,subject,content,fileName);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(@Nullable Call<String> call,
                               @Nullable Response<String> response) {
            if (response != null) {
                if (response.isSuccessful()) {
                    MainActivity.ComObj.ShowToast(activity_new_ticket.this, response.body() + "", Toast.LENGTH_SHORT);
                    ResetWidjet();
                    pd.cancel();
                } else {
                    try {
                        assert response.errorBody() != null;
                        MainActivity.ComObj.ShowToast(activity_new_ticket.this, response.errorBody().string(), Toast.LENGTH_LONG);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    pd.cancel();
                }
            }
        }

        @Override
        public void onFailure(@Nullable Call<String> call,@Nullable Throwable t) {
            if (t != null) {
                MainActivity.ComObj.ShowToast(activity_new_ticket.this, t.getMessage(), Toast.LENGTH_SHORT);
            }
            pd.cancel();
        }
    });

}

希望这对您有帮助