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);
        }

    }
}


<strong>UPDATE</strong> I assign body.toString() to variable, somehow it worked.

String response = body.string(); BaseApiDto errorDto = adapter.fromJson(response);

Answer1:

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);

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

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