我想将json对象发送到服务器,并将Retrofit作为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对象
那我怎么发送jsonObejct?
注意:如果我使用@Body发送为json对象 但是我不能将@Body与@MultiPart一起使用!
答案 0 :(得分:0)
回答这个问题有点晚了。我希望在这里发布答案会有用。
如您在问题中所说,
但是此请求将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 */ });