2

I am trying to send data which will appear in this form:

{
    "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": 
}

I have made the interface like this using retrofit:

 @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

    );

And it is posted using rxjava like this:

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


    }

However when I try to post it I get this error: 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)

EDIT:

However logging shows that the venue is created:

 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"}

How do I post the data? I have done it successfully with other means so I know that the problem is not on the backend. What is causing the error?

The venue POJO is like this:

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



}
zacmwa
  • 578
  • 10
  • 28

2 Answers2

1

you can send it as custom object you can make in your app that's will be better than fields

and can make it like this

@POST("api/events/")
Call<ResponseBody> events(@Body CustomObject obj);

and your object can make nested of anything you want

Sattar
  • 2,453
  • 2
  • 33
  • 47
0
@Headers("Content-Type: application/json; charset=utf-8")
@POST("api/events/")
Call<ResponseBody> events(@Body String body);

send json in body

Solution 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

    );
Piyush Patel
  • 371
  • 1
  • 5
  • 13
  • I can't do that because I am posting multipart form data. – zacmwa Feb 14 '17 at 13:43
  • Then do something like this String jsonString=""venue": { "id": , "name": "", "city": "", "address": "", "rating": null, "point": null }"; RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString); – Piyush Patel Feb 15 '17 at 05:23
  • I have tried that and I have still gotten the same error. – zacmwa Feb 15 '17 at 11:35
  • It has these parsers "application/json", "application/x-www-form-urlencoded", "multipart/form-data" – zacmwa Feb 15 '17 at 11:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135762/discussion-between-piyush-patel-and-zacmwa). – Piyush Patel Feb 15 '17 at 11:55