Retrfoit:使用@Multipart将jsonObject作为@RequestBody发送

问题描述 投票:5回答:1

我想通过Retrofit将json对象发送到服务器作为RequestBody

 {"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对象

那我怎么能像jsonObject一样发送?

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

java android retrofit2 okhttp3
1个回答
0
投票

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

正如你在问题中所说,

但是这个请求发送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。

您仍然可以使用Multipart上传发布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 */ });
© www.soinside.com 2019 - 2024. All rights reserved.