本文介绍了使用retrofit2 Put方法上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的retrofit2。我想通过api服务上传图像作为文件。我已经通过选择一个文件,邮递员试图找到它的工作。但是通过手机,我怎样才能上传图像文件。



这里是Api的Header部分

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

在body部分中,图片,名称,电子邮件和电话



我想要,



我创建了一个具有body参数的类
UpdateRequest Class

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

//也为参数生成Getters和Setters



$ $ $ $ $ $

Api接口函数

  public interface MediaUploadApiInterface {

@Headers({ b $ bcontent-type:multipart / form-data; boundary = ---- WebKitFormBoundary7MA4YWxkTrZu0gW
})
@PUT(api / employee)
Call< UpdateResponse> updateDetails(@Body UpdateRequest请求,@Header(X-TOKEN)字符串标记);






$ b现在我可以如何绑定细节并发送。请帮助我。

解决方案

您必须创建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 parametersUpdateRequest 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.

这篇关于使用retrofit2 Put方法上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 11:13