本文介绍了java.io.EOFException:Gson解析器中第1行第1列路径$的输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Gson和Retrofit解析JSON字符串.我有这个JSON字符串:
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)
知道如何获取message
字段值的人们请帮助我.
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);
}
}
}
更新,我以某种方式将body.toString()
分配给了变量.
UPDATE I assign body.toString()
to variable, somehow it worked.
String response = body.string();
BaseApiDto errorDto = adapter.fromJson(response);
推荐答案
之所以奏效,是因为我没有两次致电body.string()
.
It worked because I didn't call body.string()
twice.
我以某种方式将body.toString()
分配给了变量.
I assign body.toString()
to variable, somehow it worked.
String response = body.string();
BaseApiDto errorDto = adapter.fromJson(response);
这篇关于java.io.EOFException:Gson解析器中第1行第1列路径$的输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!