Retrofit 2.0 multipart

跟風遠走 提交于 2019-12-05 20:27:17

I also had so much trouble making this kind of request to work. I end up using this to upload an Image or a Video:

@Multipart
@POST(Constants.URL_UPLOAD)
Call<ResponseBody> upload(@PartMap Map<String, RequestBody> params);

And the method API call

private String uploadFile(String path, final String type) throws IOException, JSONException {

    Map<String, RequestBody> map = new HashMap<>();
    map.put("Id", Utils.toRequestBody("0"));
    map.put("Name", Utils.toRequestBody("example"));
    String types = path.substring((path.length() - 3), (path.length()));

    if (path != null) {
        File file2 = new File(path);
        RequestBody fileBody = RequestBody.create(MediaType.parse(type), file2);
        map.put("file\"; filename=\"cobalt." + types + "\"", fileBody);
    }

    Call<ResponseBody> call = cobaltServices.upload(map);
    ResponseBody response = call.execute().body();

    return RESULT_OK;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!