我将继续学习改造,并希望处理服务器的响应。
邮递员的回复结构
{
"succeeded": false,
"errors": [
{
"code":"DuplicateUserName",
"description":"User name 'XXX' is already taken."
},
{
"code": "PasswordTooShort",
"description": "Passwords must be at least 6 characters."
},
{
"code": "PasswordRequiresLower",
"description": "Passwords must have at least one lowercase ('a'-'z')."
},
{
"code": "PasswordRequiresUpper",
"description": "Passwords must have at least one uppercase ('A'-'Z')."
}
]
}
开机自检代码
Call<RegisterResponseModel> register(@Body UserJSONModel user);
RegisterResponseModel
private String succeeded;
private ArrayList<String> errors;
我尝试使用List,ArrayList以及String和Evenn序列化:
@SerializedName("errors")
@Expose
但是无论尝试如何,我都会收到成功= false和错误列表
‹ í˝`I–%&/mĘ{JőJ×ŕtˇ€`$Ř�@ěÁ�Íć’ěiG#)«*�ĘeVe]f@Ě흼÷Ţ{ď˝÷Ţ{ď˝÷ş;ťN'÷ß˙?\fdlöÎJÚÉž!€ŞČ?~|?"~ńGÍz:ÍóY>űčŃyV6ů裼®«şůčŃ÷~ńGÓj–ô裧ëUYLł6˙ŞÉëŮ"˙hôŃ,o¦u±j‹jI-đEş¤oŇŹ_eçă´hҬ¬ólvť¶ŮŰ|9ţč—|˙—
答案 0 :(得分:0)
不要像成功案例那样处理此案例。如果后端返回错误,则需要像错误一样处理它。 我像这样对okHttp使用自定义拦截器
class ErrorInterceptor : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
if (<Check is repsonce fail>) {
val body = response.body()
body?.let(::parseBackendError) ?: throw IOException("Body is null")
}
return response
}
private fun parseBackendError(responseBody: ResponseBody) {
<Parce your error list and throw exeption>
}
}
答案 1 :(得分:0)
您可以使用ArrayList错误解析该json;
import java.util.List;
公共类RegisterResponseModel {
private boolean succeded;
private List<Error> errors;
public boolean isSucceded() {
return succeded;
}
public void setSucceded(boolean succeded) {
this.succeded = succeded;
}
public List<Error> getErrors() {
return errors;
}
public void setErrors(List<Error> errors) {
this.errors = errors;
}
@Override
public String toString() {
return "TestDTO [succeded=" + succeded + ", errors=" + errors + "]";
}
}
public class Error {
private String code;
private String description;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Error [code=" + code + ", description=" + description + "]";
}
}
答案 2 :(得分:0)
将此添加到您的帖子方法@FormUrlEncoded