gpt4 book ai didi

java - 如何在改造中以多部分形式发送 POJO 形式的嵌套 json 数据?

转载 作者:行者123 更新时间:2023-11-30 10:33:24 25 4
gpt4 key购买 nike

我正在尝试发送将以这种形式出现的数据:

{
"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 错误

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)

编辑:

但是日志记录显示 field 已创建:

 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);

你的对象可以嵌套任何你想要的东西

关于java - 如何在改造中以多部分形式发送 POJO 形式的嵌套 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42227395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com