52143

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

  • Gson Map Key Pair to json
  • Volley.NoConnectionError : java.io.EOFException 0
  • GSON deserialize an array of complex objects
  • HikariCP hanging on getConnection
  • How to know when data is loaded inside custom RecyclerView?
  • Add to stack from ArrayList (Java)
  • Peek MSMQ message with infinite timeout
  • Using @Before aspect on @Controller methods with Spring doesn't work
  • RecyclerView Dynamic Header
  • Android ListFragment update/refresh and onItemClick
  • StreamWriter won't flush to NetworkStream
  • How to correctly deal with escaped Unicode Characters in R's library RJSONIO when reading json
  • How to use getFilter form Listadapter
  • PXAction seemingly does nothing
  • typescript: any difference between number[] and [number,number]?
  • How Can I Prevent Activation For Some ListView Items When The Selection Mode Is MultiChoiceModal?
  • BizTalk Party not being resolved for incoming HL7v2 message
  • Unicorn and Rails eat up 2x MySQL connections
  • Knockout custom binding handler
  • aapt.exe'' finished with non-zero exit value 1
  • Android app gives error “BatteryStatsImpl: reading network stats”
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Installing iPhone App to iPhone
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Abort upload large uploads after reading headers
  • SignalR .NET Client Invoke throws an exception
  • Retrieving value from sql ExecuteScalar()
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Data Validation Drop Down Box Arrow Disappearing
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • Hits per day in Google Big Query
  • How to stop GridView from loading again when I press back button?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#