簡體   English   中英

如何使用改造2發送帖子對象

[英]How to send post Object with retrofit 2

我在向我的API發送發布請求時遇到了麻煩。 我需要以這種形式發送數據

userinfo:{  
   Name="sample",
   Company="sample",
   Contact="Contact"
},
Password="",
PasswordConfirm="",
TokenStr=""
}

我需要使用@Body注釋發送我的請求嗎? 這是我的示例代碼(更新)

ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        User userReg = new User("asd","asd","bryce@email.com","asd",
                "asd","asd","asd","1");
        UserRegistration sendReg = new UserRegistration(userReg,"asd","asd",
                token);
        final Call<UserRegistration> tableCall = apiService.getUserRegistration(sendReg);
        tableCall.enqueue(new Callback<UserRegistration>() {
            @Override
            public void onResponse(Call<UserRegistration> call, Response<UserRegistration> response) {
                int statusCode = response.code();
                if (response.isSuccessful()){
                    Log.e("asdasd",response.body().toString());
                }else{
                    Log.e("asdas",response.message());
                }
            }

            @Override
            public void onFailure(Call<UserRegistration> call, Throwable t) {
                t.printStackTrace();
                Log.e("ERROR",""+t.getMessage()+" "+t.getLocalizedMessage());
                Toast.makeText(getApplicationContext(), ""+t.toString(), Toast.LENGTH_LONG).show();
            }
        });

這是我的ApiInterface(更新)

@POST("UserRegistration")
    @FormUrlEncoded
    Call<UserRegistration> getUserRegistration(@Body UserRegistration userRegistration);

我添加了一個模型類

public class UserRegistration {

    User items;
    String Password;
    String PasswordConfirm;
    String TokenStr;

    public UserRegistration(User items, String password, String passwordConfirm, String tokenStr) {
        this.items = items;
        Password = password;
        PasswordConfirm = passwordConfirm;
        TokenStr = tokenStr;
    }
}

public class User {

    private String COMPANY;
    private String CONTACT_NO;
    private String EMAIL;
    private String FNAME;
    private String LNAME;
    private String MI;
    private String POSITION;
    private String RID;

    public User(String COMPANY, String CONTACT_NO, String EMAIL, String FNAME, String LNAME, String MI, String POSITION, String RID) {
        this.COMPANY = COMPANY;
        this.CONTACT_NO = CONTACT_NO;
        this.EMAIL = EMAIL;
        this.FNAME = FNAME;
        this.LNAME = LNAME;
        this.MI = MI;
        this.POSITION = POSITION;
        this.RID = RID;
    }
}

現在,我收到一條錯誤消息,指出@Body參數不能與表單或多部分編碼一起使用。 (參數1)。

但是我收到不好的要求。 我知道我的要求是錯誤的。 而且我是android新手。 我嘗試研究如何執行此操作,但未找到任何有用的答案。

  1. 您提供的JSON有點不對,請更正

  2. 根據您的JSON進行POJO

  3. 更改getUserRegistration()ApiInterface

@POST(“帳戶/驗證”)

通話記錄(@Body RegisterRequest registerRequest)

您可以參考這篇文章: https : //futurestud.io/tutorials/retrofit-send-objects-in-request-body

  1. 您可以這樣使用@Body:
  2. @POST調用getUserRegistration(@Body Userinfo userinfo);
  3. 為您的json建立模型,並將其作為參數傳遞。

暫無
暫無

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

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