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

时间:2016-03-14 13:02:46

标签: java android image-uploading multipart okhttp

我正在尝试使用okhttp v3.2.0上传包含多个图像的多部分数据(可以是无数字或最多4个数字),主要问题是,除图像之外的所有数据都上传到服务器。我在其食谱页面上尝试了源代码okhttp,我也尝试了Uploading a large file in multipart using OkHttpFile upload with okhttp 但没有找到任何成功。该应用程序没有崩溃,没有错误。但图像不会上传。我还使用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 个答案:

答案 0 :(得分:2)

首先,您的代码中存在一些错误

检查mediatype检查位置,始终是相同的列表元素(应该是索引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等名称吗?