发送 ArrayList<String> 作为 Retrofit Android 的一部分

huangapple 未分类评论46阅读模式
英文:

Sending ArrayList<String> as part using Retrofit Android

问题

尝试在图像和其他细节旁边发送一个ArrayList

在选择复选框时获取列表项

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;

服务器响应

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

接口类

@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)
        );

除了ArrayList之外,其他所有内容都被发送到服务器。

如何将ArrayList作为一个部分传递?

英文:

Trying to send an ArrayList<String> along side an image and other details

Getting the list item when a checkBox is selected

ArrayList&lt;String&gt; intrest = new ArrayList&lt;&gt;();
 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

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

Interface class


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


Map&lt;String, RequestBody&gt; partMap = new HashMap&lt;&gt;();
        for (int i = 1; i &lt; interest.size(); i++) {
            partMap.put(&quot;interest&quot;, createPartFromString(interest.get(i)));
        }

 Call&lt;ResponseBody&gt; call = RetrofitClient.getInstance()
                .getApi()
                .createPost(
                        createPartFromString(title),
                        createPartFromString(link),
                        partMap,
                        prepareFilePart(&quot;image&quot;, selectedImage)
                );

Every other thing get sent to the server except the ArrayList<String>.

How do a pass an ArrayList<String> as a part?

答案1

得分: 0

(我尚未尝试过,但这应该可行)

更改请求原型定义,以便interest是一个Part而不是PartMap

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

然后将interest作为字符串列表传入。无需将字符串转换为RequestBody,Retrofit会为您完成。

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

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

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

@Part(&quot;interest&quot;) List&lt;String&gt; 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&lt;ResponseBody&gt; call = RetrofitClient.getInstance()
            .getApi()
            .createPost(
                    createPartFromString(title),
                    createPartFromString(link),
                    interest,
                    prepareFilePart(&quot;image&quot;, selectedImage)
            );

huangapple
  • 本文由 发表于 2020年4月6日 20:37:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61060044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定