Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.4.0 New line in String causes retrofit.RetrofitError: java.io.EOFException #397

Closed
mazurio opened this issue Feb 1, 2014 · 59 comments
Closed

Comments

@mazurio
Copy link

mazurio commented Feb 1, 2014

Gist: https://gist.github.com/mazurio/8750846

Description is a text taken from TextView (Android), if it contains more than one new line then it causes EOFException and insert fails. Trying to figure it out now.

I tried to escape the string. It is the same problem.

@mazurio
Copy link
Author

mazurio commented Feb 1, 2014

I have found out what causes the problem, this is ANDROID AOSP bug. More here:

http://stackoverflow.com/questions/19258518/android-httpurlconnection-eofexception
http://stackoverflow.com/questions/15411213/android-httpsurlconnection-eofexception
http://stackoverflow.com/questions/12319194/android-httpurlconnection-throwing-eofexception

I am not really sure how it should be "fixed" in RetroFit. It was already fixed in AOSP but the bug is still there in older versions.

@mazurio mazurio closed this as completed Feb 1, 2014
@mazurio mazurio reopened this Feb 1, 2014
@JakeWharton
Copy link
Collaborator

Are you saying that one of the fields in the Announcement instance contains a newline which causes the request to throw the EOFException? Can you tell me what version of Android you are seeing this on, whether or not you are using OkHttp, and if you are using the default serialization or supplying a custom Converter?

@mazurio
Copy link
Author

mazurio commented Feb 3, 2014

Hi Jake,

Yes indeed, one of the fields in the Announcement or basically any String causes that. I tried both 4.1 and 4.4.2 KitKat. I am using default serialization. I am not so sure if I am using any OkHttp.

Also: it's random, sometimes it works, sometimes it doesn't even with the same data.

@JakeWharton
Copy link
Collaborator

Thanks I'll try to repro soon. If you are able to turn on logging (setLogging(FULL)) and capture the full request body that'd also help.

@swanson
Copy link

swanson commented Feb 7, 2014

I tested this out in my app. API 18, Retrofit 1.4.1, OkHttp 1.3.0 - and everything seems to be okay when POSTing fields with multiple new lines, though, as you mention, it is intermittent so it's possible I just haven't hit it.

The request body looked something like this (some data redacted, sorry):

02-07 13:21:31.562: D/Retrofit(2445): ---> HTTP POST http://localhost:8080/Api/Mailbox/Message
02-07 13:21:31.562: D/Retrofit(2445): Content-Type: application/json; charset=UTF-8
02-07 13:21:31.562: D/Retrofit(2445): Content-Length: 135
02-07 13:21:31.562: D/Retrofit(2445): {"Body":"Test with multiple newlines\n\n\nAsdf\n\n\nAsdf\n\n\n\n\n\n\nAsdfasdf","Subject":"Test","To":3}
02-07 13:21:31.562: D/Retrofit(2445): ---> END HTTP (135-byte body)

@artworkad
Copy link

retrofit 1.4.1, I noticed the same exception. I tested on nexus 4 with android 4.4.2, no issues. Then I tested on samsung galaxy s3 mini with android 4.1.9 and got the eof exception (only for post requests). This happens randomly, but must of the time.

Further I noticed, that I have to retry the async call when getting this exception. On second try it will do the rest call.

03-11 19:56:37.445    8476-8476/d.. W/System.err﹕ retrofit.RetrofitError: java.io.EOFException
03-11 19:56:37.445    8476-8476/d.. W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:420)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:262)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:313)
03-11 19:56:37.455    8476-8476/d.. W/System.err﹕ at retrofit.CallbackRunnable.run(CallbackRunnable.java:38)
03-11 19:56:37.455    8476-8476/d.. W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at retrofit.Platform$Android$2$1.run(Platform.java:136)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ Caused by: java.io.EOFException
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at libcore.io.Streams.readAsciiLine(Streams.java:203)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
03-11 19:56:37.455    8476-8476/d... W/System.err﹕ at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
03-11 19:56:37.465    8476-8476/d... W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
03-11 19:56:37.465    8476-8476/d... W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
03-11 19:56:37.465    8476-8476/d... W/System.err﹕ at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:94)
03-11 19:56:37.465    8476-8476/d.... W/System.err﹕ at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:49)
03-11 19:56:37.465    8476-8476/d... W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:357)
03-11 19:56:37.465    8476-8476/d...W/System.err﹕ ... 7 more

👍 for a fix @JakeWharton

My dirty fix:

@Override
public void failure(RetrofitError error) {
    if(error.toString().contains("java.io.EOFException")){
        doAgain();
    }else{
       handleFailure();
   }
}

@jeffdcamp
Copy link

I'm also seeing this same issue randomly (on POST requests). I never see this on a device running Android 4.4.2 (including Nexus and Galaxy S4), but I'm seeing this on several devices running Android 4.3 or less (including the Android 4.3 Emulator).

It seems that if I do back-to-back calls (using the very same call), that we see this issue happen. It actually appears that it happens on every-other call (the first call succeeds, the next call throws an EOFException, then the next call succeeds, repeat). I'm assuming that something is getting reset after the Exception.

Update: I'm seeing this with only POST and PUT back-to-back calls that contain @Body.

Should I be resetting something with retrofit between calls?

FYI... The only work around is to watch for the "EOFException" and the retry (EOFException happens on every other call)

@gwvatieri
Copy link

@jeffdcamp @artworkad do you have update for this? Having same problem, it happens often but randomly.

@artworkad
Copy link

@gwvatieri check for exception and retry, this is the only fix at the moment

@Override
public void failure(RetrofitError error) {
    if(error.toString().contains("java.io.EOFException")){
        doAgain();
    }else{
       handleFailure();
   }
}

@ansgarm
Copy link

ansgarm commented Apr 14, 2014

Or include and use OkHttp. That worked for me if I remember correctly.

@gwvatieri
Copy link

I have seen that @artworkad, I'll give it a try, thanks. @ansgarm was OkHttp used by default when present?

@ansgarm
Copy link

ansgarm commented Apr 14, 2014

Yes, it is. Simply adding it to my gradle build files solved the problem
for me.

@gwvatieri
Copy link

@ansgarm I'll include it and try. Can't reproduce it systematically so not sure how to test it.

@jeffdcamp
Copy link

My only work-around is to retry (just like what @artworkad mentioned) (kinda painful to put that everywhere)

@gwvatieri
Copy link

Hey @jeffdcamp have you tried to include OKHttp? @ansgarm problem with cache after using it?

@theblang
Copy link

I experienced this issue on a Nexus 5 running 4.3. It would seem to occur after some time had passed since the last request. It would always work the second time and it only seemed to occur with POST requests. @gwvatieri I switched over to OKHttp and wow, my requests were about fifty percent faster. I didn't see this issue anymore after switching. I just .setClient(new OKClient()) when building the RestAdapter.

@gwvatieri
Copy link

Hey @mattblang you don't need to set it manually, it's done automatically, you just need to include the library in your classpath. I did it and it looks like the issue is no longer happening, I'll confirm it later.

@Override Client.Provider defaultClient() {

@kaciula
Copy link

kaciula commented Apr 16, 2014

For me, it's still happening with the okhttp library included in the dependencies. (Nexus 4, Android 4.4)

rubenlop88 pushed a commit to rubenlop88/autotracks-android that referenced this issue Apr 28, 2014
@jeffdcamp
Copy link

I finally got around to a situation that I was reproducing this error on every-other-call and I finally added the following to my project:

compile 'com.squareup.okhttp:okhttp:1.5.4'

By doing so, it seems to help quite a bit with this issue (I don't hardly ever see the same error when doing a POST now).

@theDazzler
Copy link

I also am getting this error when issuing a POST request with String data from a multitext EditText in Android. it occurs every time for me, not every other time.

@JakeWharton
Copy link
Collaborator

Still can't repro. Seeing an actual request body that causes this would help greatly.

@jeffdcamp
Copy link

For us.... this only happens with the following:

  • Only on POST or PUT requests
  • The body is a single line JSON string
  • back-to-back POST or PUT requests
  • You CANNOT depend on okhttp:1.5.x or use okhttp
  • The device must be Android 4.3 or less

as stated before.... our work-around to this issue is to simply depend on okhttp:1.5.4.

@pflammertsma
Copy link

It appears that this problem has disappeared with Retrofit 1.6.x. Is anybody still experiencing this?

@marcelpinto
Copy link

I just get it with 1.6.1

Caused by: java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)

@hrafnthor
Copy link

Currently using Retrofit 1.6.0 and getting a EOFException intermittently while doing POST. Not using OKHttp, running on Android 4.2.2 when it happens.

@hongzhou85
Copy link

This is very bad performance for the server side, can you imagine one person make 2-3 request to succeed, and you multiply that by the number of users and their actions.

@Mashnin
Copy link

Mashnin commented Aug 12, 2014

Issue not fixed. I'm using the latest Retrofit, and occasionally get exceptions mentioned before.
Tried to use Retrofit with and without OkHttp.
Log when using OkHttp matches the one that hongzhou85 posted.
Log without OkHttp:
java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy2.fetchConfiguration(Native Method)
...

@marcelpinto
Copy link

Are you using okhttp 2.0?

I used to have this error and after adding it, it never happened again.

Marcel Pintó
El 12/08/2014 18:08, "Mashnin" notifications@github.com escribió:

Issue not fixed. I'm using the latest Retrofit, and occasionally get
exceptions mentioned before.
Tried to use Retrofit with and without OkHttp.
Log when using OkHttp matches the one that hongzhou85 posted.
Log without OkHttp:
java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
at
libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
at
libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:486)
at
retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy2.fetchConfiguration(Native Method)
...


Reply to this email directly or view it on GitHub
#397 (comment).

@Mashnin
Copy link

Mashnin commented Aug 12, 2014

As I said, I tried to use it, but error still happens.

@BackPackerDz
Copy link

I have the same bug and using okhttp doesn't solve my problem.

08-13 10:09:29.121: D/Retrofit(19633): java.io.EOFException
08-13 10:09:29.121: D/Retrofit(19633): at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:154)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371)
08-13 10:09:29.121: D/Retrofit(19633): at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
08-13 10:09:29.121: D/Retrofit(19633): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-13 10:09:29.121: D/Retrofit(19633): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-13 10:09:29.121: D/Retrofit(19633): at retrofit.Platform$Android$2$1.run(Platform.java:142)
08-13 10:09:29.121: D/Retrofit(19633): at java.lang.Thread.run(Thread.java:856)
08-13 10:09:29.121: D/Retrofit(19633): ---- END ERROR

@glenwong
Copy link

I'm running into the same issue. It's happening whether or not I use okhttp 2.0 or normal httpurlconnection. Only seems to occur on POST and only when I have an empty post body.

@hongzhou85
Copy link

@JakeWharton Hi Jake, are u able to reopen and look into this issue since it seems like it is not resolved simply by using OKHttp2.0.

@hboregio
Copy link

I was having the same problem while doing a GET. In my case, what was triggering this error was the fact that this was an authenticated GET and I was encoding my key/secret using Base64.encodeToString() to generate the corresponding header. The problem was that the generated string contained a new line (\n) inside of it, causing the server to misbehave and return a null response. This null response was responsible for this EOFException. The solution was to simple remove the \n from the encoded string.

I know this is a specific problem to my app, but since there doesn't seem to be a consensus on how to reproduce this, I guess any sort of feedback helps.

@edoardotognoni
Copy link

+1

@pomber
Copy link

pomber commented Sep 15, 2014

It looks that the more robust option is to retry the request.
What is the best way to retry a request that failed with the EOFException?
Could it be done from an ErrorHandler?

kordianbruck added a commit to TUM-Dev/Campus-Android that referenced this issue Oct 8, 2014
@wlzch
Copy link

wlzch commented Oct 22, 2014

Working well now after using okhttp

@everald
Copy link

everald commented Nov 5, 2014

Just run into this error on different 4.4.x Devices.
Log:

HTTP POST http://www.app.koeln/api/test
Content-Type: application/json; charset=UTF-8
Content-Length: 80
{"brand":"56","location_radius":"26","location_zip":"50933","power_from":"4400"}
END HTTP (80-byte body)
ERROR http://www.app.koeln/api/test
java.io.EOFException
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:154)
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189)
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466)
    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
    at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
    at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
    at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at retrofit.Platform$Android$2$1.run(Platform.java:142)
    at java.lang.Thread.run(Thread.java:811)
END ERROR

A request made with a rest client yields following headers.

Status Code: 200
Date: Wed, 05 Nov 2014 13:48:28 GMT
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
Status: 200 OK
Connection: keep-alive
X-Request-Id: fdc00992-bcf2-474a-b48b-cfedd1134b0b
X-Runtime: 0.231811
Server: nginx
X-Frame-Options: ALLOW
Content-Type: application/json
Access-Control-Allow-Origin: *
Cache-Control: max-age=1800
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type,Accept
Expires: Wed, 05 Nov 2014 14:18:28 GMT

Tried with 'com.squareup.okhttp:okhttp:2.0.0' on the classpath. Any solution yet except the retry again one?

@thib-rdr
Copy link

thib-rdr commented Nov 6, 2014

Hi, same problem here, with 1.7.1, and Samsung Galaxy Tab GT-P5110.

Here is the log :

http://pastebin.com/CGj7p8pZ

@aigc-in-all
Copy link

Troubled by the death of me, how to solve?

@thib-rdr
Copy link

I solved it by switching to OkHttp.

@aigc-in-all
Copy link

@GaryHost

I just used okhttp 2.1.0 version, there are still problems, help look?

11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920): java.io.EOFException
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:192)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:78)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:665)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:429)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:374)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:221)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
11-27 21:46:59.192: E/com.wumii.android.model.service.UnreadNotificationService(22920):     at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)

@ar-g
Copy link

ar-g commented Dec 2, 2014

I got this error and switch to okhttp did not help.

java.io.EOFException
            at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:192)
            at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
            at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:78)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:665)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:429)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:374)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:469)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
            at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
            at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
            at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:811)

versions:

    compile 'com.squareup.okio:okio:1.0.1'
    compile 'com.squareup.okhttp:okhttp:2.1.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
    compile 'com.squareup.retrofit:retrofit:1.8.0'

@GaryHost What is your version of okhttp?

@thib-rdr
Copy link

thib-rdr commented Dec 4, 2014

@grub-
Here are my versions :

compile 'com.squareup.retrofit:retrofit:1.7.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'

@johnjohndoe
Copy link
Contributor

I can reproduce the error for every POST request I send. Here is my project setup. Please let me know if I can add any further information.

app/build.gradle:

compile 'com.squareup.dagger:dagger:1.2.2'
provided 'com.squareup.dagger:dagger-compiler:1.2.2'
compile 'com.squareup.retrofit:retrofit:1.8.0'
compile 'com.squareup.retrofit:converter-jackson:1.8.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.0'
compile 'io.reactivex:rxandroid:0.23.0'

ReportsService.java:

public interface ReportsService {
    @POST("/")
    public Observable<CreateReportResponse> createReport(
            @Body Report report
    );
}

ApiModule.java:

public final class ApiModule {

    public static final String API_URL = "http://100.200.100.200:8000";
    public static final String REPORTS = "REPORTS";


    @Provides
    @Singleton
    @Named(REPORTS)
    Endpoint provideReportsEndpoint() {
        return Endpoints.newFixedEndpoint(API_URL);
    }

    @Provides
    @Singleton
    ObjectMapper provideObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(
                PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        return objectMapper;
    }

    @Provides
    @Singleton
    @Named(REPORTS)
    ReportsService provideReportsService(
            @Named(REPORTS) Endpoint endpoint,
            ObjectMapper objectMapper) {
        return createRestAdapterBuilder(objectMapper)
                .setEndpoint(endpoint)
                .build()
                .create(ReportsService.class);
    }

    private RestAdapter.Builder createRestAdapterBuilder(ObjectMapper objectMapper) {
        return new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new JacksonConverter(objectMapper))
                .setRequestInterceptor(request -> {
                    request.addHeader("Accept", "application/json");
                });
    }

}

Report.java:

public class Report {

    public String userName;
    public String emailAddress;

    public Report(String userName, String emailAddress) {
        this.userName = userName;
        this.emailAddress = emailAddress;
    }
}

fragment_new_report.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/new_report_user_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_user_name"
            android:layout_gravity="center_horizontal" />

        <EditText
            android:id="@+id/new_report_email_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email_address"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>
</ScrollView>

NewReportFragment:

protected void onSubmitReport() {
    Editable userName = userNameEditText.getText();
    Editable emailAddress = emailAddressEditText.getText();
    Report report = new Report(
            userName.toString(),
            emailAddress.toString());
    createReport(report);
}

protected void createReport(final Report Report) {

    Observable<CreateReportResponse> responseObservable =
            mReportsService.createReport(report);
    AndroidObservable.bindFragment(this, responseObservable)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<CreateReportResponse>() {
                @Override
                public void onCompleted() {
                    Log.d(getClass().getName(), "Completed createReport");
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(getClass().getName(), e.getMessage());
                }

                @Override
                public void onNext(CreateReportResponse createReportResponse) {
                    Log.d(getClass().getName(), createReportResponse.toString());
                }
            });
}

This produces the following an EOFException on the client:

              Retrofit  D  ---> HTTP POST http://100.200.100.200:8000/
                        D  Accept: application/json
                        D  Content-Type: application/json; charset=UTF-8
                        D  Content-Length: 64
                        D  {"email_address":"test@test.com","user_name":"John"}
                        D  ---> END HTTP (64-byte body)
                        D  ---- ERROR http://100.200.100.200:8000/
                        D  java.io.EOFException: \n not found: size=0 content=...
                        D      at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)
                        D      at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
                        D      at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
                        D      at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:791)
                        D      at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:678)
                        D      at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:431)
                        D      at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:376)
                        D      at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:471)
                        D      at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
                        D      at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
                        D      at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
                        D      at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
                        D      at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
                        D      at retrofit.RxSupport$2.run(RxSupport.java:55)
                        D      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                        D      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                        D      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                        D      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                        D      at retrofit.Platform$Android$2$1.run(Platform.java:142)
                        D      at java.lang.Thread.run(Thread.java:841)
                        D  ---- END ERROR
ts.NewReportFragment$1  E  \n not found: size=0 content=...

When I remove OkHttp from build.gradle the following is logged:

                D  ---> HTTP POST http://100.200.100.200:8000/
                D  Accept: application/json
                D  Content-Type: application/json; charset=UTF-8
                D  Content-Length: 67
                D  {"email_address":"test@test.com","user_name":"John"}
                D  ---> END HTTP (67-byte body)
                D  ---- ERROR http://100.200.100.200:8000/
                D  java.io.EOFException
                D      at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
                D      at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
                D      at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
                D      at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
                D      at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
                D      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
                D      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
                D      at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
                D      at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
                D      at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
                D      at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
                D      at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
                D      at retrofit.RxSupport$2.run(RxSupport.java:55)
                D      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                D      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                D      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                D      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                D      at retrofit.Platform$Android$2$1.run(Platform.java:142)
                D      at java.lang.Thread.run(Thread.java:818)
                D  ---- END ERROR

AndroidRuntime  E  FATAL EXCEPTION: main
                E  java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:52)
                E      at android.os.Handler.handleCallback(Handler.java:739)
                E      at android.os.Handler.dispatchMessage(Handler.java:95)
                E      at android.os.Looper.loop(Looper.java:135)
                E      at android.app.ActivityThread.main(ActivityThread.java:5221)
                E      at java.lang.reflect.Method.invoke(Native Method)
                E      at java.lang.reflect.Method.invoke(Method.java:372)
                E      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                E      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                E  Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
                E      at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:174)
                E      at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:96)
                E      at rx.internal.operators.NotificationLite.accept(NotificationLite.java:147)
                E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:177)
                E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.access$000(OperatorObserveOn.java:65)
                E      at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:153)
                E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:45)
                E      ... 8 more
                E  Caused by: rx.exceptions.CompositeException: 2 exceptions occurred.
                E      ... 15 more
                E  Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
                E      at android.util.Log.getStackTraceString(Log.java:330)
                E      at com.android.internal.os.RuntimeInit.Clog_e(RuntimeInit.java:59)
                E      at com.android.internal.os.RuntimeInit.access$200(RuntimeInit.java:43)
                E      at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:85)
                E      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
                E      at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
                E      at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:56)
                E      ... 8 more
                E  Caused by: retrofit.RetrofitError
                E      at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:390)
                E      at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
                E      at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
                E      at retrofit.RxSupport$2.run(RxSupport.java:55)
                E      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                E      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                E      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                E      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                E      at retrofit.Platform$Android$2$1.run(Platform.java:142)
                E      at java.lang.Thread.run(Thread.java:818)
                E  Caused by: java.io.EOFException
                E      at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
                E      at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:175)
                E      at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
                E      at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:616)
                E      at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:379)
                E      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
                E      at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
                E      at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
                E      at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
                E      at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
                E      ... 9 more

I also removed all configuration such as android:singleLine="true" and android:inputType="..." from the EditText to eliminate any side effects.


I was able to reproduce this on a Nexus 5 (Android 5.0.1), Moto G 2nd Edition (Android 4.4.4), Samsung Galaxy S4 (Android 4.4.2).


I use this SimpleHTTPServer to test the POST requests. The server receives the following:

Accept: application/json
Content-Type: application/json; charset=UTF-8
Content-Length: 65
Host: 100.200.100.200:8000
Connection: Keep-Alive
Accept-Encoding: gzip

FieldStorage(None, None, '{"email_address":"test@test.com","user_name":"John"}')

@swankjesse
Copy link
Collaborator

Can you reproduce this reliably? What does your server return?

@swankjesse
Copy link
Collaborator

@johnjohndoe your simplehttpserver.py program is broken, and OkHttp is correct to fail on responses returned from it. You can see it by hitting the server with curl, which will also fail.

$ curl --data '{}' http://localhost:8000
curl: (52) Empty reply from server

@johnjohndoe
Copy link
Contributor

Oh, my fault - sorry. I just picked it up yesterday. I also tried curl but missed to notice the error.

Can you recommend another simple server which allows quick testing of HTTP requests?

@swankjesse
Copy link
Collaborator

@johnjohndoe MockWebServer.

@xuxucode
Copy link

xuxucode commented Jan 9, 2015

I got this issue sometimes on Andorid 2.3.4.

interface:

@Headers("X-API-Version: v1.0")
@POST("/arts")
void addArt(@Body ArtProduct art, Callback<ArtResource> callback);

gist: https://gist.github.com/edwardaa/443bb10abf2b812a9363

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.+'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.squareup.okhttp:okhttp:2.1.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
    compile 'com.squareup:otto:1.3.5'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.8.0'
    compile 'com.jakewharton:butterknife:6.0.0'
    compile 'io.reactivex:rxandroid:0.23.0'
    compile 'com.github.tony19:logback-android-core:1.1.1-3'
    compile 'com.github.tony19:logback-android-classic:1.1.1-3'https://gist.github.com/edwardaa/443bb10abf2b812a9363]
    compile 'org.slf4j:slf4j-api:1.7.6'
}

@johnjohndoe
Copy link
Contributor

@swankjesse Thanks for the hint - I will come back if I still experience problems. (I am still fighting to set MockWebServer up in my project.)

@JakeWharton
Copy link
Collaborator

In order to reduce the split-brain problem this bug is causing I am locking this issue version in favor of OkHttp's @ square/okhttp#1114.

@square square locked and limited conversation to collaborators Jan 9, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests