【问题标题】:Retrfoit : send jsonObject as @RequestBody with @MultipartRetrfoit:使用@Multipart 将 jsonObject 作为@RequestBody 发送
【发布时间】:2017-03-02 00:14:29
【问题描述】:

我想用 Retrofit 作为 RequestBody 发送 json 对象到服务器

 {"attach": {
    "image": {
        "height": 1473,
        "urlRef": "",
        "width": 1473
    },
    "video": {
        "duration": "4.365",
        "height": 1920,
        "thumbUrl": "",
        "urlRef": "",
        "width": 1080
    }
}
}

这是我的改造对象

  Retrofit.Builder retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create());

这是我的改装界面:

@Multipart
@POST("post/")
Observable<Response> postAttach(
        @Part("attach") RequestBody asset
        , @Part MultipartBody.Part attachment
);

并创建 RequestBody 如下:

RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(myAttach));

但此请求将 json 作为字符串而不是 json 对象发送

那么我如何发送 jsonObejct 呢?

注意:如果我使用 @Body 作为 json 对象发送 但在我不能将@Body 与@MultiPart 一起使用!

【问题讨论】:

    标签: java android retrofit2 okhttp3


    【解决方案1】:

    回答这个问题有点晚了。我希望在这里发布答案会很有用。

    正如你在问题中所说,

    但此请求将 json 作为字符串而不是 json 对象发送

    RequestBody#create() 接受以下(可能的)参数。

    1. create(@Nullable MediaType contentType, String content)
    2. create(@Nullable MediaType contentType, ByteString content)
    3. create(@Nullable MediaType contentType, byte[] content)
    4. create(@Nullable MediaType contentType, byte[] content, int offset, int byteCount)
    5. create(@Nullable MediaType contentType, File file)

    如您所见,没有接受 JSONObject 的参数类型。您需要以 String 格式传递 JSON。

    您仍然可以通过分段上传来发布 JSON 正文。

    您需要进行以下更改。

    在界面中

    @Multipart
    @POST("post/")
    Observable<Response> postAttach(
        @Part RequestBody asset,           // <==== just remove ("attach")
        @Part MultipartBody.Part attachment
    );
    

    创建以下 POJO 类。

    图像类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Image {
    
       @SerializedName("height")
       @Expose
       private int height;
       @SerializedName("urlRef")
       @Expose
       private String urlRef;
       @SerializedName("width")
       @Expose
       private int width;
    
       public Image(int height, String urlRef, int width) {
          this.height = height;
          this.urlRef = urlRef;
          this.width = width;
       }
    
       // other constructors, getter and setter methods
    }
    

    视频类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Video {
    
       @SerializedName("duration")
       @Expose
       private String duration;
       @SerializedName("height")
       @Expose
       private int height;
       @SerializedName("thumbUrl")
       @Expose
       private String thumbUrl;
       @SerializedName("urlRef")
       @Expose
       private String urlRef;
       @SerializedName("width")
       @Expose
       private int width;
    
       public Video(String duration, int height, String thumbUrl, String urlRef, int width) {
          this.duration = duration;
          this.height = height;
          this.thumbUrl = thumbUrl;
          this.urlRef = urlRef;
          this.width = width;
       }
    
       // other constructors, getter and setter methods
    }
    

    附加类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Attach {
    
       @SerializedName("image")
       @Expose
       private Image image;
       @SerializedName("video")
       @Expose
       private Video video;
    
       public Attach(Image image, Video video) {
          this.image = image;
          this.video = video;
       }
    
       // constructors, getter and setter methods
    }
    

    MyAttach 类

    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class MyAttach {
    
       @SerializedName("attach")
       @Expose
       private Attach attach;
    
       public MyAttach(Attach attach) {
          this.attach = attach;
       }
    
       // other constructors, getter and setter methods    
    }
    

    以上类在代码中的使用。

    Image image = new Image(1473, "image_url", 1473);
    Video video = new Video(4.365, 1920, "thumb_url", "video_url", 1080);
    Attach attach = new Attach(image, video);
    MyAttach attach = new MyAttach(attach);
    
    // finally serialize the above MyAttach object into JSONObject
    Gson gson = new Gson();
    String json = gson.toJson(attach);
    

    并将此 JSON 数据传递到接口中。

    RequestBody requestBody = RequestBody.create(
                             MediaType.parse("multipart/form-data"), // notice I'm using "multipart/form-data"
                             json
                             );
    

    传递给接口

    MultipartBody.Part attachment = // prepare file to upload
    // other codes here
    YourCustomResponseClassHere call = yourService.postAttach(requestBody, attachment);
    call.enqueue({ /* your implementation */ });
    

    【讨论】:

    • 如果我们必须从存储中实际发送任何图像而不是 image_url 怎么办,那么这种格式将不起作用......你能更新你的答案吗?
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 2019-11-23
    • 2020-08-04
    • 2016-05-06
    • 2019-06-04
    • 2021-08-06
    • 1970-01-01
    • 2011-07-20
    相关资源
    最近更新 更多