当api被调用时,它返回一个状态代码为201的onsccess中的对象;当不在onsuccess中时,它还返回一个状态代码为422的对象
{
"errors": [
"Usuário não encontrado"
]
}
我已经尝试了很多方法,包括从改版的官方网站使用此代码
public class ErrorUtils {
public static APIError parseError(Response<JsonObject> response) {
Converter<ResponseBody, APIError> converter =
ServiceGenerator.retrofit()
.responseBodyConverter(APIError.class, new Annotation[0]);
APIError error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
Log.v("LoginUser", "IOException -> " + e.getMessage());
return new APIError();
}
return error;
}
public static class ServiceGenerator {
public static final String API_BASE_URL = ApiClient.BASE_URL;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static Retrofit retrofit = null;
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static Retrofit retrofit() {
retrofit = builder.client(httpClient.build()).build();
return retrofit;
}
}}
api错误类
public class APIError {
private String[] errors;
public String[] getErrors() {
return errors;
}
public void setErrors(String[] errors) {
this.errors = errors;
}
@Override
public String toString() {
return "ClassPojo [errors = " + errors + "]";
}}
但通过这种方式,它进入error utils类的catch块,异常是
End of input at line 1 column 1 path $
对此问题的解决将不胜感激。但请记住,我已经仔细研究了这两个错误,然后我发布了这个问题。
1条答案
按热度按时间bbmckpt71#
请在您的计算机中尝试此代码
onResponse
回调方法你可以通过
errorStringList
以你的ApiError
```ApiError apiError = new ApiError();
apiError.setError(errorStringList.toArray(new String[0]))