When doing any POST with any async method when APIClient.setDebugging(true) we are getting an "java.net.ProtocolException: unexpected end of stream".
Setting APIClient.setDebugging(false) solves the issue, but we can't debug async POST calls.
Overriding onUploadProgress() I noticed that we get the double of bytesWritten than the contentLength.
With debugging in false
Upload progress bytesWritten=101, contentLength=101, done=true
Download progress bytesRead=51, contentLength=51, done=false
Download progress bytesRead=51, contentLength=51, done=true
With debugging in true
--> POST https://api.artik.cloud/v1.1/messages HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 101
Authorization: Bearer <user-token-removed>
Accept: application/json
User-Agent: Swagger-Codegen/2.0.5/java
Upload progress bytesWritten=101, contentLength=101, done=true
{"data":{"volume":500},"sdid":"09174ee9e12644ec91f7ff4ed0e40700","ts":1477602273950,"type":"message"}
--> END POST (101-byte body)
Upload progress bytesWritten=202, contentLength=101, done=false
Exception in thread "OkHttp Dispatcher" java.lang.AssertionError: java.net.ProtocolException: unexpected end of stream
at com.sun.tools.javac.util.Assert.error(Assert.java:133)
at cloud.artik.api.MessagesApiTest$2.onFailure(MessagesApiTest.java:192)
at cloud.artik.client.ApiClient$1.onFailure(ApiClient.java:1014)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:185)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I've tested this on swagger-codegen v2.2.2-SNAPSHOT.
@juanniosi thanks for reporting the issue. The default Java API client uses okhttp-gson. Can you try retrofit2 or jersey2 HTTP library instead to see if the issue persists?
You may find this useful: https://github.com/swagger-api/swagger-codegen/wiki/FAQ#how-can-i-generate-an-android-sdk
I am facing the similar issue. When debug is on,
java.net.ProtocolException: unexpected end of stream
error is produced at:
com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close()
@mintu19 have you tried other HTTP libraries (retrofit2, jersey2, etc) as mentioned above?
@wing328 No I have not. As i was in hurry to provide workable code, so was working with okhttp only. Will check other libraries when i get time.
I can confirm that this happens with retrofit2 as well. retrofit doesn't use the same debugging API, so that isn't enabled.
Here is a discussion related to the issue "unexpected end of stream"
https://github.com/square/okhttp/issues/2147#issuecomment-171768924
It mentions about 2 workarounds but I've not tried any of those. Please give it a try to see how it goes.
I just found what the cause of this error.
The Generated file ProgressRequestBody was call 2 times in POST.
this class keep an instance of BufferedSink and use it each time writeTo method was called
it work fine with GET requests cause writeTo is call only one time.
I was able to correct the error by changing the ProgressRequestBody.mustache template.
removed line 26 :
private BufferedSink bufferedSink;
````
modified writeTo method with :
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink bufferedSink = Okio.buffer(sink(sink));
requestBody.writeTo(bufferedSink);
bufferedSink.flush();
}
```
@Megamzero may I know if you've time to submit a PR so that we can review the fix?
This is still an issue and a serious one, as it also affected GET requests in my tests. Also despite the comments above, the state of the debug flag had no affect. The fix suggested by @Megamzero works.
$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
@talbright thanks for confirming the fix by @Megamzero works. Do you mind submitting a PR for the fix?
Closed via #5786
Most helpful comment
I just found what the cause of this error.
The Generated file
ProgressRequestBodywas call 2 times in POST.this class keep an instance of
BufferedSinkand use it each timewriteTomethod was calledit work fine with GET requests cause
writeTois call only one time.I was able to correct the error by changing the
ProgressRequestBody.mustachetemplate.removed line 26 :
```