Retrofit: java.io.EOFException: End of input at line 1 column 1 for Content-Length as 0

Created on 22 Mar 2016  路  5Comments  路  Source: square/retrofit

If the Content-Length is 0 and then Retrofit is trying to get the response in the T type which results in the exception. Here is the exception which i received it. I have used GSONConvertor for transforming the response to the Object.

If the content-length is 0 , then the library is trying to parse the content which result in exceptions.
Instead retrofit can return with NULL so that we will read the response headers.

Server can give the response headers where client can understand the error message and error code other than HTTP default error. If the server is sending error code then the content-length will be always 0.

java.io.EOFException: End of input at line 1 column 1
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1407)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:553)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:429)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:201)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

Most helpful comment

Just use Void Class accept the response(My English is poor).浣跨敤Void 绫诲幓鎺ユ敹灏卞彲浠ヨВ鍐充簡.
Example: Call\

All 5 comments

A 0-length body is not a valid JSON document and returning null potentially masks things like server failures. If you want an empty body to be coerced into null you can add this functionality yourself like so: https://github.com/square/retrofit/issues/1554#issuecomment-178633697

Thx for immediate response, I will implement the way it was given in the #1554. what kind of server failures which contains content-length as 0 with HTTP response code as 200. I am curiosity to get to know about the HTTP scenarios.
Again thanks for your immediate comments.

If a server implementation started to fail at writing a JSON response the client might get empty bodies, for example. The larger point is that the JSON serializer decided to throw because it expected more than 0 bytes (per the JSON spec). Other serializers, like protocol buffers, are actually perfectly happy with 0-byte responses which simply indicate a proto instance whose values are all absent (or set to the default).

It's up to the serializer to decide if empty is allowed, and when the serializer doesn't like empty we offer the previously linked "escape hatch" to make Retrofit handle it before the serializer ever sees it.

Just use Void Class accept the response(My English is poor).浣跨敤Void 绫诲幓鎺ユ敹灏卞彲浠ヨВ鍐充簡.
Example: Call\

Getting "null" as response as for Call.
Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column for XML response?
Please help me out?

Was this page helpful?
0 / 5 - 0 ratings