【发布时间】:2017-07-12 17:30:31
【问题描述】:
我正在尝试使用改造 2 使用休息 API,我已经能够使用一些端点,但注册端点不断返回 http 500 错误代码,但在使用邮递员测试时工作正常。
@POST("auth/signup/")
Call<SignUpResponce> addUser(@Body SignUpCreds signUpCreds);
这是注册凭据
public class SignUpCreds {
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public SignUpCreds(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}
}
这是注册回复
public class SignUpResponce {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
@SerializedName("dateRegistered")
@Expose
private Integer dateRegistered;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getDateRegistered() {
return dateRegistered;
}
public void setDateRegistered(Integer dateRegistered) {
this.dateRegistered = dateRegistered;
}
}
邮递员中的Json对象
{
"username": "doe2jane",
"email": "jane@yahoo.com",
"password": "janedoe"
}
邮递员中的 JSON 响应
{
"id": 7,
"username": "doe2jane",
"email": "jane@yahoo.com",
"password": "janedoe",
"dateRegistered": 1499870604166
}
我的注册凭据
SignUpCreds creds = new SignUpCreds(username, email, password);
改造类:
public class AuthUtil {
private static Retrofit sRetrofit = null;
public static Retrofit getRetrofit(String url){
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (sRetrofit == null){
sRetrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return sRetrofit;
}
}
邮递员屏幕:https://ibb.co/gNwrQF
【问题讨论】:
-
您能否发布 SignUpCreds 的完整代码?
auth/signup/末尾的/也有必要吗? -
这是类的全部代码,它只有一个构造函数和字段
-
使用
@Body Type name时,字段命名很重要 -
我已经更新了 SignUpCreds 类,我使用 jsonschematopojo 来生成类。删除
auth/signup末尾的黑斜线没有任何改变。 -
嗯。一切看起来都不错,您应该发布您的 Postman 请求或(理想情况下)查看您的服务器错误日志所说的内容
标签: java android json retrofit