Retrofit API Post call返回错误500,适用于Postman

问题描述 投票:1回答:2

我正在尝试使用改进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": "[email protected]",
"password": "janedoe"

}

Json在邮递员中的回应

{
"id": 7,
"username": "doe2jane",
"email": "[email protected]",
"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;
}

}

邮递员屏幕:https://ibb.co/gNwrQF

java android json retrofit
2个回答
2
投票

首先,请错误日志。

500内部服务器错误只是意味着服务器中抛出了一些异常,请求未按预期完成。所以我猜它可能是一些空指针异常,原因可能是你的无效JSON或你的代码逻辑我不确定它。


0
投票
  1. 有时,这也发生在Android客户端,没有在标题Content-Type上定义@Headers("Content-Type: application/json")
  2. 也许服务器端不接受来自android(客户端)的特定类型,因此抛出500错误代码。

类似的问题,我发现,当我们的服务器使用laravel,并有这个auth:api middleware。 android方面没有使用Content-type: application/json显式设置标头,返回500代码错误,而不是401响应代码。

e.g: invalid auth token from postman

© www.soinside.com 2019 - 2024. All rights reserved.