Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2 End of input at line 1 column 1 path $

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

like image 510
user6505882 Avatar asked Oct 29 '25 10:10

user6505882


1 Answers

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.

like image 173
Pedro Gonzalez Avatar answered Oct 30 '25 23:10

Pedro Gonzalez