Meh*_*aji 5 java android retrofit2 okhttp3
我想通过Retrofit将json对象发送到服务器作为RequestBody
{"attach": {
"image": {
"height": 1473,
"urlRef": "",
"width": 1473
},
"video": {
"duration": "4.365",
"height": 1920,
"thumbUrl": "",
"urlRef": "",
"width": 1080
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的改造对象
Retrofit.Builder retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
Run Code Online (Sandbox Code Playgroud)
这是我的改造界面:
@Multipart
@POST("post/")
Observable<Response> postAttach(
@Part("attach") RequestBody asset
, @Part MultipartBody.Part attachment
);
Run Code Online (Sandbox Code Playgroud)
并创建RequestBody如下:
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(myAttach));
Run Code Online (Sandbox Code Playgroud)
但是这个请求发送json作为字符串而不是json对象
那我怎么发送jsonObejct?
注意:如果我使用@Body发送作为json对象,但我不能将@Body与@MultiPart一起使用!
回答这个问题有点晚了。我希望在这里发布答案会有用。
正如你在问题中所说,
但此请求将 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 的参数类型。您需要以 JSONString格式传递。
您仍然可以通过分段上传来发布 JSON 正文。
您需要进行以下更改。
界面中
@Multipart
@POST("post/")
Observable<Response> postAttach(
@Part RequestBody asset, // <==== just remove ("attach")
@Part MultipartBody.Part attachment
);
Run Code Online (Sandbox Code Playgroud)
创建以下 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
}
Run Code Online (Sandbox Code Playgroud)
视频课
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
}
Run Code Online (Sandbox Code Playgroud)
附加类
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
}
Run Code Online (Sandbox Code Playgroud)
我的附加类
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
}
Run Code Online (Sandbox Code Playgroud)
在代码中使用上述类。
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);
Run Code Online (Sandbox Code Playgroud)
并将此 JSON 数据传递到接口中。
RequestBody requestBody = RequestBody.create(
MediaType.parse("multipart/form-data"), // notice I'm using "multipart/form-data"
json
);
Run Code Online (Sandbox Code Playgroud)
传递给接口
MultipartBody.Part attachment = // prepare file to upload
// other codes here
YourCustomResponseClassHere call = yourService.postAttach(requestBody, attachment);
call.enqueue({ /* your implementation */ });
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1089 次 |
| 最近记录: |