使用Retrofit 2.0的POST多部分表单数据,包括图像 [英] POST Multipart Form Data using Retrofit 2.0 including image

查看:106
本文介绍了使用Retrofit 2.0的POST多部分表单数据,包括图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Retrofit 2.0

MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");
MediaType MEDIA_TYPE_IMAGE = MediaType.parse("image/*");

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.JPEG,90,byteArrayOutputStream);
profilePictureByte = byteArrayOutputStream.toByteArray();

Call<APIResults> call = ServiceAPI.updateProfile(
        RequestBody.create(MEDIA_TYPE_TEXT, emailString),
        RequestBody.create(MEDIA_TYPE_IMAGE, profilePictureByte));

call.enqueue();

服务器返回错误,指出文件无效.

The server returns an error saying the file is not valid.

这很奇怪,因为我试图在iOS上使用相同的格式上传相同的文件(使用其他库),但是上传成功.

This is weird because I have tried to upload the same file with the same format on iOS(using other library), but it uploads successfully.

我想知道使用 Retrofit 2.0 上传图像的正确方法是什么?

I am wondering what is the proper way to upload an image using Retrofit 2.0?

在上传之前我应该​​先将其保存到磁盘吗?

Should I save it to disk first before uploading?

P.S .:我已经对其他不包含图像的Multipart请求进行了改造,并且已成功完成.问题是当我尝试向身体添加一个字节时.

P.S.: I have used retrofit for other Multipart request that does not include image and they completed successfully. The problem is when I am trying to include a byte to the body.

推荐答案

我在1.9和2.0中都强调了该解决方案,因为它对某些人很有用

I am highlighting the solution in both 1.9 and 2.0 since it is useful for some

1.9中,我认为更好的解决方案是将文件保存到磁盘并将其用作Typed文件,例如:

In 1.9, I think the better solution is to save the file to disk and use it as Typed file like:

(我不知道您的服务器端实现)具有与此类似的API接口方法

(I don't know about your server-side implementation) have an API interface method similar to this

@POST("/en/Api/Results/UploadFile")
void UploadFile(@Part("file") TypedFile file,
                @Part("folder") String folder,
                Callback<Response> callback);

并像使用它

TypedFile file = new TypedFile("multipart/form-data",
                                       new File(path));

对于RetroFit 2,请使用以下方法

RetroFit 2.0(这是固定的RetroFit 2中 issue 的一种解决方法现在,有关正确的方法,请参考 jimmy0251的答案)

API接口:

For RetroFit 2 Use the following method

RetroFit 2.0 ( This was a workaround for an issue in RetroFit 2 which is fixed now, for the correct method refer jimmy0251's answer)

API Interface:

public interface ApiInterface {

    @Multipart
    @POST("/api/Accounts/editaccount")
    Call<User> editUser(@Header("Authorization") String authorization,
                        @Part("file\"; filename=\"pp.png\" ") RequestBody file,
                        @Part("FirstName") RequestBody fname,
                        @Part("Id") RequestBody id);
}

使用方式如下:

File file = new File(imageUri.getPath());

RequestBody fbody = RequestBody.create(MediaType.parse("image/*"),
                                       file);

RequestBody name = RequestBody.create(MediaType.parse("text/plain"),
                                      firstNameField.getText()
                                                    .toString());

RequestBody id = RequestBody.create(MediaType.parse("text/plain"),
                                    AZUtils.getUserId(this));

Call<User> call = client.editUser(AZUtils.getToken(this),
                                  fbody,
                                  name,
                                  id);

call.enqueue(new Callback<User>() {

    @Override
    public void onResponse(retrofit.Response<User> response,
                           Retrofit retrofit) {

        AZUtils.printObject(response.body());
    }

    @Override
    public void onFailure(Throwable t) {

        t.printStackTrace();
    }
});

这篇关于使用Retrofit 2.0的POST多部分表单数据,包括图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆