如何使用改造2发送帖子对象

时间:2019-01-30 09:46:47

标签: android json api retrofit2

我无法向我的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新手。我尝试研究如何执行此操作,但没有找到任何有用的答案。

2 个答案:

答案 0 :(得分:0)

  1. 您提供的JSON有点错误,请更正

  2. 根据您的JSON进行POJO

  3. 更改getUserRegistration()ApiInterface,以

@POST(“帐户/验证”)

呼叫注册(@Body RegisterRequest registerRequest)

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

答案 1 :(得分:0)

  1. 您可以像这样使用@Body:
  2. @POST     致电getUserRegistration(@Body Userinfo userinfo);
  3. 为您的json建立模型并将其作为参数传递。