【问题标题】:How to Post complex datas as MultiPart using Retrofit in Android?如何在 Android 中使用 Retrofit 将复杂数据作为 MultiPart 发布?
【发布时间】:2019-11-25 21:39:41
【问题描述】:

我的数据是 Json 对象数组,我需要将其作为 MultiPart 表单数据传递。我需要发布的数据如下:

{
    "name": "KIMS EKM",
    "latitude": "8.5605418",
    "longitude": "76.8810471",
    "state": "Kerala",
    "district": "Thiruvananthapuram",
    "city": "Leela Infopark",
    "landmark": "Kazhakuttom",
    "phone": "7293318484",
    "email": "aswin.as@velosit.in",
    "admin_name": "Aswin",
    "admin_phone": "7293318484",
    "admin_email": "aswin.a.s@velosit.in",
    "departments": [{
        "id": 1,
        "is_sunday": 1
    }, {
        "id": 2,
        "is_sunday": 1
    }, {
        "id": 3,
        "is_sunday": 1
    }, {
        "id": 4,
        "is_sunday": 1
    }, {
        "id": 5,
        "is_sunday": 1
    }, {
        "id": 6,
        "is_sunday": 1
    }, {
        "id": 7,
        "is_sunday": 1
    }, {
        "id": 8,
        "is_sunday": 1
    }, {
        "id": 9,
        "is_sunday": 0
    }, {
        "id": 10,
        "is_sunday": 0
    }, {
        "id": 11,
        "is_sunday": 0
    }],
    "all_day_service": [3, 4, 5],
    "emergency_services": [5],
    "special_services": ["BPD", "HCDX"],
    "accreditations": [2, 3]
}

这里使用确切的键作为多部分表单数据的参数。 有图片需要发布,但真正的问题在下面描述。

我已经发布了 MultiPart 表单数据,但是发布 json 对象数组是我觉得很难的事情。 "departments" & "all_day_service" 字段怎么贴?

【问题讨论】:

  • 您可以在您的问题中添加您的 Web 服务接口类吗?
  • 我还没有真正创建它,但这里的问题是我应该如何为“部门”添加@Part
  • 您必须将 jsonarray 转换为字符串,然后添加到您的正文中
  • @RajasekaranM 你能帮忙吗? stackoverflow.com/questions/62783444/…

标签: android kotlin retrofit retrofit2 multipartform-data


【解决方案1】:

你必须制作数据部分的模型,它应该看起来像这样:


 @Multipart
    @POST("request")
    Call<ResponseBody> upload(
        @Part("item1") RequestBody item1,
        @Part("item2") RequestBody item2,
        @Part("item3") RequestBody item3
    );

还有@PartMap Map&lt;String, RequestBody&gt; map

【讨论】:

  • 这适用于名称、纬度、经度。但问题是我该如何为“部门”写@Part?
  • "departments" 不能只作为字符串发布。我们可以将对象数组转换为字符串并发布它。但这不是正确的方法吗?
  • 如果我理解正确,那么您需要制作一个数据模型 Department 并将 List 作为多部分发送
  • 请记住,如果您想发送字符串、整数等数组,您可以在这里找到解决方案stackoverflow.com/a/53832681/3809653
  • JSON 中的自定义对象数组怎么样!如何解决它
【解决方案2】:
    -----------------------------------com.example.Department.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Department {

@SerializedName("id")
@Expose
public Integer id;
@SerializedName("is_sunday")
@Expose
public Integer isSunday;

}
-----------------------------------com.example.ResponseContracts.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ResponseContracts {

@SerializedName("name")
@Expose
public String name;
@SerializedName("latitude")
@Expose
public String latitude;
@SerializedName("longitude")
@Expose
public String longitude;
@SerializedName("state")
@Expose
public String state;
@SerializedName("district")
@Expose
public String district;
@SerializedName("city")
@Expose
public String city;
@SerializedName("landmark")
@Expose
public String landmark;
@SerializedName("phone")
@Expose
public String phone;
@SerializedName("email")
@Expose
public String email;
@SerializedName("admin_name")
@Expose
public String adminName;
@SerializedName("admin_phone")
@Expose
public String adminPhone;
@SerializedName("admin_email")
@Expose
public String adminEmail;
@SerializedName("departments")
@Expose
public List<Department> departments = null;
@SerializedName("all_day_service")
@Expose
public List<Integer> allDayService = null;
@SerializedName("emergency_services")
@Expose
public List<Integer> emergencyServices = null;
@SerializedName("special_services")
@Expose
public List<String> specialServices = null;
@SerializedName("accreditations")
@Expose
public List<Integer> accreditations = null;

}

http://www.jsonschema2pojo.org/ 从这里,你制作一个模型,通过@body 发送请求

@POST("/api/v1/post url here")
Call<Response> registerUser(@Body ResponseContracts body);

【讨论】:

  • 由于 registerUser 方法没有使用@Multipart 注释,所以可以和多部分表单数据一起使用吗?
  • 你应该使用改造2
  • 是的。我真的在用!你能分享一下如何解决这个问题的教程吗
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 2016-05-06
  • 2017-05-14
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
相关资源
最近更新 更多