【问题标题】:Is it possible to send a String[] through Multipart using Retrofit?是否可以使用 Retrofit 通过 Multipart 发送 String[]?
【发布时间】:2019-01-04 23:54:24
【问题描述】:

我正在开发一个应用程序,其中用户必须从列表中选择任意数量的国家,并且我必须通过多部分发送选定的名称。

我没有与 String[] 一起上传任何文件,但是没有多部分的信息就无法上传信息,而且我对 Web 服务器的运行方式没有任何意见。

我试图简单地将它作为数组、ArrayList 和 JsonArray 发送:

@Headers({
    "Connection: Keep-Alive",
})
@Multipart
@PUT("/user/{id}")
String updateUser(@Path("id") int userId, @Part("user[countries]") String[] countries);

我也尝试过this solution,但我要么误解了它,要么它不起作用。这是我尝试使用的代码:

ArrayList<String> countries = CountryManager.getInstance().getSelectedCountryIds();
RequestBody requestBody;
LinkedHashMap<String, RequestBody> hashMap = new LinkedHashMap<>();

for(int i = 0; i < countries.size(); i++) {
    requestBody = RequestBody.create(MediaType.parse("text/plain"), countries.get(i));
    hashMap.put("countries["+i+"]", requestBody);
}

并相应地改变改造方法:

@Headers({
    "Connection: Keep-Alive",
})
@Multipart
@PUT("/user/{id}")
String updateUser(@Path("id") int userId, @PartMap() Map countries);

但是,在所有尝试中,我都得到了错误 retrofit.RetrofitError: Part body must not be null。

我还提到改造的 ChangeLog 提到“新:支持使用 OkHttp 的 MultipartBody.Part 的可迭代和数组 @Part 参数”,但经过一番挖掘,我发现给定的示例相当混乱,以至于我不确定如何在我的代码中实现它,并且无法找到甚至提到在多部分中发送数组的教程。

这样的事情根本不可能吗?

【问题讨论】:

  • 使用 PartMap 注解的解决方案似乎是对的。我在一些应用程序中使用它并且工作正常。你确定所有国家都不为空吗?

标签: android multipartform-data retrofit2


【解决方案1】:

我知道我迟到了这个答案。您可以使用@Query("someKey[]") 在多部分中发送arraylist。这是工作示例。

@Multipart
    @POST("./")
    Call<JsonElement> addSubEvent(@Part(EndAPI.USE_CASE) RequestBody useCase,
                                  @Query("event_id[]") ArrayList<String> event_id,
                                  @Query("user_id[]") ArrayList<String> user_id,
                                  @Query("name[]") ArrayList<String> name,
                                  @Query("date_time[]") ArrayList<String> date_time,
                                  @Part("token") RequestBody token,
                                  @Part MultipartBody.Part... profilePic);

希望对寻求答案的人有所帮助。

【讨论】:

  • 如何在php中解析arraylist
【解决方案2】:

创建多部分列表以用作数组列表

List<MultipartBody.Part> descriptionList = new ArrayList<>();
descriptionList.add(MultipartBody.Part.createFormData("param_name_here", values));

以下是改造服务接口中的功能。

@PUT("/")
@Multipart
Call<ResponseBody> uploadPhotos(
        @Part MultipartBody.Part placeId,
        @Part MultipartBody.Part name,
        @Part List<MultipartBody.Part> desclist, // <-- use such for list of same parameter
        @Part List<MultipartBody.Part> files  // <-- multiple photos here
);

希望它可以帮助某人。干杯... !!!

【讨论】:

  • 它适用于通过 Multipart 传递字符串列表。谢谢@Siddher
  • 节省了我很多时间,感谢您的解决方案!
【解决方案3】:

只需使用@Part("items[]") 列出项目

像这样:

List<String> items = new ArrayList();
    items.add("1");
    items.add("2");
    items.add("3");

@Multipart
@POST("/")
Call<Result> yourMethod(@PartMap() Map<String, RequestBody> partMap, @PartMap() Map<String, RequestBody> map, @Part MultipartBody.Part file, @Part("items[]") List<String> items);

【讨论】:

  • 如何在php中解析这个数组??
【解决方案4】:

这是可能的,也可以使用多部分,只需将您的数组转换为 Json 字符串并将其添加为单个部分,一旦在服务器端接收到此 Json,只需对其进行反序列化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多