![](/img/trans.png)
[英]how do i create POJO class for json with dynamic keys for retrofit?
[英]How do I send nested json data in POJO form in a multipartform in retrofit?
我正在尝试发送将以这种形式显示的数据:
{
"id": ,
"venue": {
"id": ,
"name": "",
"city": "",
"address": "",
"rating": null,
"point": null
},
"name": "",
"time": "",
"event_pic": null,
"description": "",
"event_type": "Movie",
"invite_only": ,
"free": ,
"age_restriction": ,
"ticket_price": ,
"user":
}
我使用改造制作了这样的界面:
@Multipart
@POST("api/events/")
Observable<Event> postEvent(
@Part("venue") Venue venue,
@Part("event_pic") RequestBody image,
@Part("name") RequestBody name,
@Part("description") RequestBody description,
@Part("time") RequestBody date,
@Part("event_type") RequestBody type,
@Part("invite_only") RequestBody isInviteOnly,
@Part("age_restriction") RequestBody isAgeRestricted,
@Part("free") RequestBody isFree,
@Part("ticket_price") RequestBody ticketPrice
);
它是使用 rxjava 发布的,如下所示:
public void postEvent() {
postEventUseCase.setAgeRestricted(ageRestricted);
postEventUseCase.setDate(date);
postEventUseCase.setFree(free);
postEventUseCase.setInviteOnly(inviteOnly);
postEventUseCase.setDescription(description);
postEventUseCase.setName(name);
postEventUseCase.setPath(path);
postEventUseCase.setTicketprice(ticketprice);
postEventUseCase.setType(type);
postEventUseCase.setVenue(venue);
subscription = postEventUseCase.execute().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
}
但是,当我尝试发布它时,我收到此错误:02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: <-- 400 Bad
Request http://zacmwa.pythonanywhere.com/api/events/ (7417ms)
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Server: openresty/1.9.15.1
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Date: Tue, 14 Feb 2017 13:21:26 GMT
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Content-Type: application/json
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Transfer-Encoding: chunked
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Connection: keep-alive
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Vary: Accept
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: X-Frame-Options: SAMEORIGIN
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: Allow: GET, POST, OPTIONS
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: {"venue":["This field is required."]}
02-14 16:21:26.468 7133-7762/com.wyat.wyat D/OkHttp: <-- END HTTP (37-byte body)
编辑:
然而,日志显示场地已创建:
02-14 16:21:18.620 7133-7762/com.wyat.wyat D/OkHttp: Content- Disposition: form-data; name="venue"
02-14 16:21:18.620 7133-7762/com.wyat.wyat D/OkHttp: Content-Transfer-Encoding: binary
02-14 16:21:18.620 7133-7762/com.wyat.wyat D/OkHttp: Content-Type: application/json; charset=UTF-8
02-14 16:21:18.620 7133-7762/com.wyat.wyat D/OkHttp: Content-Length: 59
02-14 16:21:18.620 7133-7762/com.wyat.wyat D/OkHttp: {"address":"ersysaj","city":"ahgsagya","name":"hdyfjfnfjf"}
我如何发布数据? 我已经用其他方式成功地做到了,所以我知道问题不在后端。 导致错误的原因是什么?
场地POJO是这样的:
public class Venue {
@SerializedName("name")
@Expose
private String name;
@SerializedName("city")
@Expose
private String city;
@SerializedName("address")
@Expose
private String address;
@SerializedName("rating")
@Expose
private Double rating;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Venue withName(String name) {
this.name = name;
return this;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
}
你可以将它作为自定义对象发送,你可以在你的应用程序中制作这比字段更好
并且可以做到这样
@POST("api/events/")
Call<ResponseBody> events(@Body CustomObject obj);
你的对象可以嵌套任何你想要的东西
@Headers("Content-Type: application/json; charset=utf-8")
@POST("api/events/")
Call<ResponseBody> events(@Body String body);
在正文中发送 json
解决方案2:
MultipartBody.Part typedFile = MultipartBody.Part.createFormData("event_pic",imagefile.getName(),RequestBody.create(MediaType.parse("image"), imagefile));
String jsonString=""venue": { "id": , "name": "", "city": "", "address": "", "rating": null, "point": null }";
RequestBody venue = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
@POST("api/events/")
Observable<Event> postEvent(
@Part("venue") RequestBody body,
@Part MultipartBody.Part image,
@Part("name") RequestBody name,
@Part("description") RequestBody description,
@Part("time") RequestBody date,
@Part("event_type") RequestBody type,
@Part("invite_only") RequestBody isInviteOnly,
@Part("age_restriction") RequestBody isAgeRestricted,
@Part("free") RequestBody isFree,
@Part("ticket_price") RequestBody ticketPrice
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.