英文:
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<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<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("interest") List<String> interest,
Then pass interest in as a list of strings. There's no need to convert the strings into RequestBody
s, Retrofit does it for you.
Call<ResponseBody> call = RetrofitClient.getInstance()
.getApi()
.createPost(
createPartFromString(title),
createPartFromString(link),
interest,
prepareFilePart("image", selectedImage)
);
专注分享java语言的经验与见解,让所有开发者获益!
评论