Retrofit API Post调用返回错误500,适用于Postman(Retrofit API Post call returning error 500, works well with Po

编程入门 行业动态 更新时间:2024-10-09 22:18:08
Retrofit API Post调用返回错误500,适用于Postman(Retrofit API Post call returning error 500, works well with Postman)

我正在尝试使用改造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.

更多推荐

本文发布于:2023-07-19 15:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适用于   错误   Post   Retrofit   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!