我正在尝试使用改进 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
}
我的 signUpCred
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;
}
}
2 回答
首先,请错误日志。
500 内部服务器错误只是意味着服务器中抛出了一些异常,请求未按预期完成。所以我猜它可能是一些空指针异常,原因可能是你的无效 JSON 或你的代码逻辑我不确定它。
有时,这也发生在一个 Android 客户端,没有在标题
@Headers("Content-Type: application/json")
或_上定义Content-Type
也许服务器端不接受来自 android(客户端)的特定类型,因此抛出 500 错误代码。
类似的问题我发现,当我们的服务器使用 laravel,并有这个auth:api 中间件。 android 方面没有使用
Content-type: application/json
显式设置标头,返回 500 代码错误,而不是 401 响应代码。