私の知る限り,,レトロフィットのONFAILUREはありません、インターネット接続などのエラーを処理するために使用されます。
サーバーからのエラー応答、エラー応答を処理するには、4xxステータスコードを持つサーバーからの応答を意味しますが、それを処理するクライアント用のJSON応答があります。
言って、あなたがサーバーからこのエラー構造を得ている:
{
statusCode: 409,
message: "Email address already registered"
}
このエラーはonResponse(...)
にキャプチャされます。これに対処するには、作成あなたの
public class ErrorBean {
private int statusCode;
private String message;
public ErrorBean() {
}
public int status() {
return statusCode;
}
public String message() {
return message;
}
}
簡単なのErrorHandlerのUTILを作成します。
public class ErrorUtils {
public static ErrorBean parseError(Response<?> response) {
Converter<ResponseBody, ErrorBean> converter =
ServiceGenerator.retrofit()
.responseBodyConverter(ErrorBean.class, new Annotation[0]);
ErrorBean error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
return new ErrorBean();
}
return error;
}
}
そして最後に、
...
call.enqueue(new Callback<SuccessResponse>() {
@Override
public void onResponse(Call<SuccessResponse> call, Response<SuccessResponse> response) {
if (response.isSuccessful()) {
// use response data and do some fancy stuff :)
} else {
// parse the response body …
ErrorBean error = ErrorUtils.parseError(response);
// … and use it to show error information
// … or just log the issue like we’re doing :)
Log.d("error message", error.message());
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
// there is more than just a failing request (like: no internet connection)
}
});
は、あなたがポイントを得たホープ.. !!!
http://stackoverflow.com/questions/35029936/centralized-error-handling-retrofit-2が役立ちます。 – Amir