使用Body进行Retrofit2 POST请求

时间:2015-10-29 14:50:29

标签: android retrofit

我需要使用参数" guid = 1"制作POST请求。在身体。我使用Retrofit2

我试试:

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

用户类:

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

如何提出此请求?

2 个答案:

答案 0 :(得分:1)

您必须致电call.enqueue,提供Callback< ApiModelJson>的实例,您将获得回复。 enqueue异步执行后端调用。您可以详细了解call.enqueue here

答案 1 :(得分:0)

使用以下代码,您可以同步发出请求:

ApiModelJson responseBody = call.execute();

如果您希望它是异步的:

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

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