我正在尝试使用改造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; }}
邮递员屏幕: https : //ibb.co/gNwrQF
I am trying to consume a rest API using retrofit 2, i have been able to consume some endpoint but the sign up endpoint keeps returning http 500 error code but works fine when tested with postman. @POST("auth/signup/") Call<SignUpResponce> addUser(@Body SignUpCreds signUpCreds);
This is the sign up credentials
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; }}
This is the sign up response
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 object in postman
{ "username": "doe2jane", "email": "jane@yahoo.com", "password": "janedoe"}
Json Response in postman
{ "id": 7, "username": "doe2jane", "email": "jane@yahoo.com", "password": "janedoe", "dateRegistered": 1499870604166}
My signUpCred
SignUpCreds creds = new SignUpCreds(username, email, password);
Retrofit Class:
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; }}
Postman screen: https://ibb.co/gNwrQF
最满意答案
首先,请错误日志。
500内部服务器错误只是意味着服务器中抛出了一些异常,请求未按预期完成。 所以我猜它可能是一些空指针异常,原因可能是你的无效JSON或你的代码逻辑我不确定它。
first, please the error logs.
500 Internal server error simply mean some exception has been thrown in server and request not completed as expected. So I guess its may be some null pointer exception and the reason might be your invalid JSON or your code logic I am not sure about it.
更多推荐
发布评论