Multipart Application Method Using Android Retrofit2

Asked 1 years ago, Updated 1 years ago, 101 views

I am working on replacing the communication API with retrofit2Only

Communicating plain text parameters works well

Blocked in placing and uploading image.

The result keeps failing, but I don't know where the problem is.

Below is the Android source code, and I would appreciate it if you could answer where it is wrong or missing<

==================================================================

    ..... ApiInterface.class

    @Multipart
    @POST(BaseVariable.URL_INSERT)
    Call<Results> postInsert(@PartMap Map<String, RequestBody> params);


    ..... ..... MainActivity.class

    Map<String, RequestBody> map = new HashMap<>();
    map.put("user_id", toRequestBody("user_id")); // User ID value (string)

    File = newFile(thumbnailFile.getPath()); // The address of the image file is verified
    RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file); 
    map.put("url_upload", fileBody);

    Call<Results> repos = MainApplication.getApiInterface().postInsert(map);
    repos.enqueue(new Callback<Results>() {
        @Override
        public void onResponse(Response<Results> response) {
        .....

android retrofit2 multipart

2022-09-21 17:39

1 Answers

Looking at the GitHub issue below, retrofit 1.Unlike x, @Part("file\"; filename=\"photo.It seems that an annotation should be added in a format such as png") RequestBody file.

If you use the map like the code you posted, I think you can handle it as follows. (The filename photo.png is optional.)

map.put("url_upload\"; filename=\"photo.png\"", fileBody);

Or you can use MultipartBody.Part, but MultipartBody.Part is required to hand over the file name when it is created in the first place.


2022-09-21 17:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.