我无法向我的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新手。我尝试研究如何执行此操作,但没有找到任何有用的答案。
答案 0 :(得分:0)
您提供的JSON有点错误,请更正
根据您的JSON进行POJO
更改getUserRegistration()
在ApiInterface
,以
@POST(“帐户/验证”)
呼叫注册(@Body RegisterRequest registerRequest)
您可以参考这篇文章:https://futurestud.io/tutorials/retrofit-send-objects-in-request-body
答案 1 :(得分:0)