0

Trying to send an ArrayList along side an image and other details

Getting the list item when a checkBox is selected

ArrayList<String> intrest = new ArrayList<>();
 switch (buttonView.getId()) {
            case R.id.business:
                if (business.isChecked()) {
                    interest.add(String.valueOf(business.getText()));
                }
                break;
            case R.id.politics:
                if (politics.isChecked()) {
                    interest.add(String.valueOf(politics.getText()));
                }
                break;
            case R.id.entertainment:
                if (entertainment.isChecked()) {
                    interest.add(String.valueOf(entertainment.getText()));
                }
                break;

Server Response

{"message":"Created  successfully","data":{"interest":[],"showAds":true,"description":"ghbjbjj",
"title":"title","link":"link","image": "imageURL"}}

Interface class


@Multipart
    @POST("/api/post")
    Call<ResponseBody> createPost(
            @Part("description") RequestBody description,
            @Part("title") RequestBody title,
            @Part("link") RequestBody link,
            @PartMap Map<String, RequestBody> interest,
            @PartMap Map data,
            @Part MultipartBody.Part adsImage
    );
@NonNull
    private RequestBody createPartFromString(String descriptionString) {
        return RequestBody.create(descriptionString,
                okhttp3.MultipartBody.FORM);
    }


Map<String, RequestBody> partMap = new HashMap<>();
        for (int i = 1; i < interest.size(); i++) {
            partMap.put("interest", createPartFromString(interest.get(i)));
        }

 Call<ResponseBody> call = RetrofitClient.getInstance()
                .getApi()
                .createPost(
                        createPartFromString(title),
                        createPartFromString(link),
                        partMap,
                        prepareFilePart("image", selectedImage)
                );

Every other thing get sent to the server except the ArrayList.

How do a pass an ArrayList as a part?

steveOS
  • 125
  • 1
  • 12

1 Answers1

0

(I haven't tried it, but this should work)

Change the request prototype definition so that the interest is a Part not PartMap:

@Part("interest") List<String> interest,

Then pass interest in as a list of strings. There's no need to convert the strings into RequestBodys, Retrofit does it for you.

Call<ResponseBody> call = RetrofitClient.getInstance()
            .getApi()
            .createPost(
                    createPartFromString(title),
                    createPartFromString(link),
                    interest,
                    prepareFilePart("image", selectedImage)
            );
Joni
  • 108,737
  • 14
  • 143
  • 193
  • I tried your solution and it sent the interest to the server but the format is not right. `"title": "Title", "interest": [ "\"Business\"", "\"Politics\"", "\"Technology and Gadgets\"" ]` expected outcome `"title": "Title", "interest": [ "Business", "Politics", "Technology and Gadgets"]` @Joni Sorry for late response. – steveOS Apr 11 '20 at 20:29