I have the following classes using the Retrofit 2 and are accusing the error trying to parse the server error.
Error
java.io.EOFException: End of input at line 1 column 1 path $
APIError.java
public class APIError {
private int statusCode;
private String message;
public APIError() {
}
public int status() {
return statusCode;
}
public String message() {
return message;
}
}
ErrorUtils.java
public class ErrorUtils {
public static APIError parseError(Response<?> response) {
Converter<ResponseBody, APIError> converter =
HttpConnectionManager.retrofit
.responseBodyConverter(APIError.class, new Annotation[0]);
APIError error;
try {
error = converter.convert(response.errorBody()); //<-- Error line
} catch (IOException e) {
return new APIError();
}
return error;
}
/**
* Configure response service error to show in the app.
* @param response
*/
public static void configResponseError(Context context, Response<JsonObject> response) {
APIError error = ErrorUtils.parseError(response);
if(response.message() != null && response.errorBody() != null) {
AppUtils.showErrorDialog((Activity) context, "Error: " + error.message() +
"\n\nStatus: " + error.status());
} else {
AppUtils.showErrorDialog((Activity) context, context.getString(R.string.ws_error));
}
Log.e(AppConstants.ERROR, "onResponse error message: " + error.message());
Log.e(AppConstants.ERROR, "onResponse error status: " + error.status());
}
}
MyCallWs:
Call<JsonObject> call = methodService.createAccount(appUserJson);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful() && response.body() != null) {
JsonObject responseJson = response.body();
//TODO: Success
} else {
ErrorUtils.configResponseError(MyActivity.this, response);
}
progressDialog.dismiss();
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
Follow this step by step to catch the error in Retrofit 2
It means your request is expecting a JsonObject in the answer, but the request doesn't return anything, only result codes (200,...), there the parsing error message. Replace JsonObject with Void (or Observable<Response<Void>> if using RxJava) and it should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With