Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.EOFException: End of input at line 1 column 1 path $ in Gson parser

I'm parsing a JSON string by using Gson and Retrofit. I have this JSON string:

{"message":["Email has already been taken"]}

I get the below exception still and don't know why:

java.io.EOFException: End of input at line 1 column 1 path $
    at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1393)
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:549)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:425)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205)
    at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:260)
    at com.google.gson.TypeAdapter.fromJson(TypeAdapter.java:273)

People who know how to get the value of message field please help me.

BaseApiDto.java

public class BaseApiDto {

    @SerializedName("message")
    public String[] message;

    public String getError() {
        return message[0];
    }

}

HandErrorUtils.java

public static void handleError(FragmentActivity activity, Throwable e) {
    String msg = null;
    if(e instanceof HttpException){
        // Error message in json
        Gson gson = new Gson();
        TypeAdapter<BaseApiDto> adapter = gson.getAdapter(BaseApiDto.class);
        ResponseBody body = ((HttpException) e).response().errorBody();

        // Status code
        HttpException httpException = (HttpException) e;
        int statusCode = httpException.code();

        if (statusCode == 500) {
            showErrorDialog(activity, activity.getString(R.string.dialog_msg_error_401), true);
        } else if (statusCode == 401) {
            showErrorDialog(activity, activity.getString(R.string.dialog_msg_error_401), true);
        } else {
            try {
                Timber.w("body.string() " + body.string());

                // TODO : EXCEPTION HAPPEN IN HERE
                BaseApiDto errorDto = adapter.fromJson(body.string());

                msg = errorDto.getError();

                Timber.w("msg " + msg);
            } catch (Exception ex) {
                // TODO : EXCEPTION HAPPEN IN HERE
                ex.printStackTrace();
            }

            showErrorDialog(activity, msg, false);
        }

    }
}

UPDATE I assign body.toString() to variable, somehow it worked.

String response = body.string();

BaseApiDto errorDto = adapter.fromJson(response);
like image 500
Huy Tower Avatar asked Apr 18 '17 09:04

Huy Tower


1 Answers

It worked because I didn't call body.string() twice.

I assign body.toString() to variable, somehow it worked.

String response = body.string();

BaseApiDto errorDto = adapter.fromJson(response);
like image 147
Huy Tower Avatar answered Oct 12 '22 00:10

Huy Tower