简体   繁体   English

java.io.EOFException:Gson解析器中第1行第1行路径$的输入结束

[英]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. 我正在使用Gson和Retrofit解析JSON字符串。 I have this JSON string: 我有这个JSON字符串:

{"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. 知道如何获得message字段价值的人请帮助我。

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. 更新我将body.toString()分配给变量,不知何故它工作。

String response = body.string();

BaseApiDto errorDto = adapter.fromJson(response);

It worked because I didn't call body.string() twice. 它工作,因为我没有两次调用body.string()

I assign body.toString() to variable, somehow it worked. 我将body.toString()分配给变量,不知何故它起作用了。

String response = body.string();

BaseApiDto errorDto = adapter.fromJson(response);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gson输出com.google.gson.JsonSyntaxException:java.io.EOFException:第1行第501列的输入结束 - Gson outputting com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 501 如何解决 java.io.EOFException: End of input at line 1 column 1 path 1 in retrofit service call in android? - How to solve java.io.EOFException: End of input at line 1 column 1 path 1 in retrofit service call in android? java.io.EOFException:Android 改造中第 1 行第 1 列路径 $ 处的输入结束 - java.io.EOFException: End of input at line 1 column 1 path $ in Android retrofit java.io.EOFException:由于输入结束,没有内容可以映射到Object - java.io.EOFException: No content to map to Object due to end of input java.io.EOFException:ZLIB 输入流意外结束 - java.io.EOFException: Unexpected end of ZLIB input stream 读取输入抛出 java.io.EOFException - Reading an input throws java.io.EOFException java.io.EOFException:由于检索数据时输入结束,没有内容映射到对象 - java.io.EOFException: No content to map to Object due to end of input while retrieving data java.io.EOFException:读取gzip编码网站的ZLIB输入流意外结束 - java.io.EOFException: Unexpected end of ZLIB input stream reading gzip encoded website java.io.EOFException:ZLIB输入流的意外结尾读取文件 - java.io.EOFException: Unexpected end of ZLIB input stream reading a file java.io.EOFException:ZLIB 输入 stream 的意外结束 - java.io.EOFException: Unexpected end of ZLIB input stream during reading stream data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM