回答这个问题有点晚了。我希望在这里发布答案会很有用。
正如你在问题中所说,
但此请求将 json 作为字符串而不是 json 对象发送
RequestBody#create() 接受以下(可能的)参数。
create(@Nullable MediaType contentType, String content)
create(@Nullable MediaType contentType, ByteString content)
create(@Nullable MediaType contentType, byte[] content)
create(@Nullable MediaType contentType, byte[] content,
int offset, int byteCount)
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 */ });