且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用retrofit2 Put方法上传图片

更新时间:2023-01-03 18:20:20

您必须创建Multipart Request

 档案档案=新档案(filePath); 

RequestBody reqFile = RequestBody.create(MediaType.parse(image / *),file);
MultipartBody.Part body = MultipartBody.Part.createFormData(picture,file.getName(),reqFile);
RequestBody name = RequestBody.create(MediaType.parse(text / plain),picture);

RequestBody name = RequestBody.create(MediaType.parse(text / plain),your_name);
RequestBody email = RequestBody.create(MediaType.parse(text / plain),your_email);
RequestBody phone = RequestBody.create(MediaType.parse(text / plain),your_phone);

HasMap< String,RequestBody> map = new HasMap<>();
map.put(name,name);
map.put(email,email);
map.put(phone,phone);

$ b postImage(map,picture)

  @Multipart 
@PUT(/)
呼叫< ResponseBody> postImage(@PartMap Map< String,RequestBody> map,@Part MultipartBody.Part image);



$ b $ p
$ b

另外,不要忘记在界面中添加@Multipart。 p>

I am new to retrofit2. I want upload image through api service as a file. I have tried with postman by selecting a file and found its working. But through mobile how can I upload the image file.

Here is the Header section of Api

--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \

In the body section I am having, "picture", "name", "email" and "phone"

I am trying like,

i have created a class with body parameters UpdateRequest Class

    public class UpdateRequest {
        @SerializedName("picture")
        MultipartBody.Part picture;
        @SerializedName("name")
        public String name;
        @SerializedName("email")
        public String email;
        @SerializedName("phone")
        public String phone;

//Also Generated Getters and Setters for the parameters    

    }

Api Interface function

public interface MediaUploadApiInterface {

    @Headers({
            "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
    })
    @PUT("api/employee")
        Call<UpdateResponse> updateDetails(@Body UpdateRequest request, @Header("X-TOKEN") String token);

}

Now How can i bind the details and send.

Please Help me..

You have to create Multipart Request like below.

File file = new File(filePath);

RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), reqFile);
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "picture");

RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "your_name"); 
RequestBody email = RequestBody.create(MediaType.parse("text/plain"), "your_email"); 
RequestBody phone = RequestBody.create(MediaType.parse("text/plain"), "your_phone"); 

HasMap<String,RequestBody> map = new HasMap<>();
map.put("name",name);
map.put("email",email);
map.put("phone",phone);


postImage(map, picture)

Interface is like below.

@Multipart
    @PUT("/")
    Call<ResponseBody> postImage(@PartMap Map<String, RequestBody> map, @Part MultipartBody.Part image);
}

Also, don't forget to add @Multipart in your interface.