使用 okhttp v3.2.0 上传多个图像文件

Uploading multiple image files using okhttp v3.2.0

我正在尝试使用 okhttp v3.2.0 上传具有多张图片(可以是 none 或最多 4 张)的多部分数据,主要问题是,除了图片之外的所有数据都上传到服务器.我在它的食谱页面上尝试了 okhttp 的源代码,我也尝试了 and 但没有找到任何成功。该应用程序没有崩溃,没有错误。但是图片没有上传。我还使用 POSTMAN 检查了服务器端 API,它工作正常,没有任何问题。这是我的代码,如有任何帮助,我们将不胜感激。

String URL = BASE_URL + "PostRequest.php";
MediaType MEDIA_TYPE;

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String PostDate = sdf.format(c.getTime());

mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(40, TimeUnit.SECONDS)
                .build();

MultipartBody.Builder mRequestBody = new MultipartBody.Builder()
    .setType(MultipartBody.FORM);
    mRequestBody.addFormDataPart("SECURITYCODE",SECURITY_CODE);
    mRequestBody.addFormDataPart("EMAIL", Email);
    mRequestBody.addFormDataPart("CATEGORY", Category);
    mRequestBody.addFormDataPart("SUBCATEGORY", SubCategory);
    mRequestBody.addFormDataPart("TITLE", Title);
    mRequestBody.addFormDataPart("DESCRIPTION", Description);
    mRequestBody.addFormDataPart("LOCATION", Location);
    mRequestBody.addFormDataPart("POSTDATE", PostDate);
    mRequestBody.addFormDataPart("LOCALITY", Locality);
    mRequestBody.addFormDataPart("TOTALIMAGES", Imagepaths.size()+"");

if (Imagepaths.size() > 0) {
    File file = new File(Imagepaths.get(0));
    if (file.exists()) {
        Log.d("file exist", "yes");
    }
    MEDIA_TYPE = Imagepaths.get(0).endsWith("png") ?    
        MediaType.parse("image/png") : MediaType.parse("image/jpeg");
    RequestBody imageBody = RequestBody.create(MEDIA_TYPE, file);
    mRequestBody.addFormDataPart("IMAGE1", "IMAGE1", imageBody);
}

if (Imagepaths.size() > 1) {
    File file = new File(Imagepaths.get(1));
    MEDIA_TYPE = Imagepaths.get(0).endsWith("png") ? 
        MediaType.parse("image/png") : MediaType.parse("image/jpeg");
    RequestBody imageBody = RequestBody.create(MEDIA_TYPE, file);
    mRequestBody.addFormDataPart("IMAGE2", "IMAGE2", imageBody);
}

if (Imagepaths.size() > 2) {
    File file = new File(Imagepaths.get(2));
    MEDIA_TYPE = Imagepaths.get(0).endsWith("png") ? 
        MediaType.parse("image/png") : MediaType.parse("image/jpeg");
    RequestBody imageBody = RequestBody.create(MEDIA_TYPE, file);
    mRequestBody.addFormDataPart("IMAGE3", "IMAGE3", imageBody);
}

if (Imagepaths.size() > 3) {
    File file = new File(Imagepaths.get(3));
    MEDIA_TYPE = Imagepaths.get(0).endsWith("png") ? 
        MediaType.parse("image/png") : MediaType.parse("image/jpeg");
    RequestBody imageBody = RequestBody.create(MEDIA_TYPE, file);
    mRequestBody.addFormDataPart("IMAGE4", "IMAGE4", imageBody);
}

RequestBody rb = mRequestBody.build();

Request request = new Request.Builder()
    .url(URL)
    .post(rb)
    .build();   

try {
    Response mResponse = mOkHttpClient.newCall(request).execute();
        if (!mResponse.isSuccessful()) throw new IOException();

        responseMsg = mResponse.body().string();

} catch (IOException e) {
    responseMsg = timeoutMessage;
}

return responseMsg;

首先你的代码中有几个错误

检查媒体类型检查位置,总是相同的列表元素(应该是索引 1、2、3 等)

   if (Imagepaths.size() > 1) {
        File file = new File(Imagepaths.get(1));

        MEDIA_TYPE = Imagepaths.get(0).endsWith("png") ? 
                MediaType.parse("image/png") : MediaType.parse("image/jpeg");


        RequestBody imageBody = RequestBody.create(MEDIA_TYPE, file);
        mRequestBody.addFormDataPart("IMAGE2", "IMAGE2", imageBody);
    }

其次,mRequestBody.addFormDataPart("IMAGE2", "IMAGE2", imageBody); 你的图片有名称 IMAGE1、IMAGE2 等吗?