【问题标题】:Retrofit: Multipart with json改造:带 json 的多部分
【发布时间】:2019-01-07 11:03:11
【问题描述】:

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

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

谁能帮我解决这个问题?

我的 JSON ::

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

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

【问题讨论】:

    标签: android json retrofit retrofit2 multipart


    【解决方案1】:

    试试这个

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

    适用于所有设备

    【讨论】:

    • 谢谢让我试试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多