Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

ruby - Multipart Image upload Retrofit Android

want to make api call in android retrofit in the following form, what is the way of sending multipart images in the following format enter image description here

my problem is when I try to send this request get unable to return parcel due to could not find or build blob: expected attachable error from the server but its fine on the postman, problem arise when I try to send images from retrofit in android

question from:https://stackoverflow.com/questions/65886357/multipart-image-upload-retrofit-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this

INTERFACE
@POST("/Your ENDPOINT")
public Call<ResponseBody> sendImages(@Body body: RequestBody)

MAKE YOUR BODY

MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("order_id",orderId)
                .addFormDataPart("return_attributes[qr_code]",qrCode)
                .addFormDataPart("return_attributes[reason]","100")
                .addFormDataPart("return_attributes[description]","(31/12/2020 13:30:00)");

    for (File file:listFiles){
        builder.addFormDataPart("return_attributes[images]",file.getName(),
                RequestBody.create(MediaType.parse("image/*"),
                        file.getAbsolutePath()));
    }

BUILD AND ENQUEUE YOUR REQUEST

RequestBody body=builder.build();

Service service = retrofit.create(ApiAlfa.class);
ApiAlfa.sendImages(body).enqueue(...);

you can see this too


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...