簡體   English   中英

帶有主體的Retrofit2 POST請求

[英]Retrofit2 POST request with Body

我需要在正文中使用參數“ 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();
        }

如何提出這個要求?

您必須調用call.enqueue ,提供Callback< ApiModelJson>的實例,您將在其中獲得響應。 enqueue異步執行您的后端調用。 您可以在此處閱讀有關call.enqueue更多信息

使用下面的代碼,您可以同步發出請求:

ApiModelJson responseBody = call.execute();

如果您希望它是異步的:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM