首页 > 解决方案 > 改造:带 json 的多部分

问题描述

我想发送带有 Multipart 表单数据的 json。

我熟悉标题 + 多部分表单数据,但标题问题不允许发送中文/希伯来语字符。所以我需要为此使用json。

谁能帮我解决这个问题?

我的 JSON ::

{
        "agent": "ee",
        "phone": "123",
        "manager": "234"
}

具有带有“图像”参数的 Multipart 的表单数据。

标签: androidjsonretrofitretrofit2multipart

解决方案


尝试这个

    RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), selectedImage /* file name*/);
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", selectedImage.getName(), fileBody);

agent = RequestBody.create(MediaType.parse("text/plain"), "<agent-value>");
phone = RequestBody.create(MediaType.parse("text/plain"), "<phone-value>");
manager = RequestBody.create(MediaType.parse("text/plain"), "<manager-value>");

在你的界面类中

  @Multipart
@POST(UPDATE_PROFILE_IMAGE)
Call<JsonObject> updateImage(@Part MultipartBody.Part image,
                             @Part("agent") RequestBody agent,
                             @Part("phone") RequestBody phone,
                             @Part("manager") RequestBody manager);

它适用于所有设备


推荐阅读