FredGan
FredGan

Reputation: 407

Retrofit2 POST request with Body

I need make POST request with parameters "guid=1" in Body. i use Retrofit2

I try :

@POST("/api/1/model")
Call<ApiModelJson> getPostClub(@Body User body);

User Class:

public class User {
     @SerializedName("guid")
     String guid;
public User(String guid ) {
     this.guid = guid;

}

MailActivity:

User user =new User ("1");
Call<ApiModelJson> call = service.getPostClub(user);
call.enqueue(new Callback<ApiModelJson>() {
        @Override
        public void onResponse(Response<ApiModelJson> response) {
}
        @Override
        public void onFailure(Throwable t) {
            dialog.dismiss();
        }

How make this request?

Upvotes: 0

Views: 2541

Answers (2)

H&#233;ctor
H&#233;ctor

Reputation: 26084

With code below, you can make the request synchronously:

ApiModelJson responseBody = call.execute();

If you want it to be asynchronous:

call.enqueue(new Callback<ApiModelJson>() {
    @Override
    public void onResponse(Response<ApiModelJson> response, Retrofit retrofit) {
    }

    @Override
    public void onFailure(Throwable t) {
    }
});

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

you have to call call.enqueue, providing an instance of Callback< ApiModelJson>, where you will get the response. enqueue executes your backend call asynchronously. You can read more about call.enqueue here

Upvotes: 1

Related Questions