2

I am using HttpsURLConnection which uses okhttp, and everytime I use PUT and send a payload of around 5KB it sends it without any errors; nevertheless, when I send larger payloads of over 10KB, I would get the following error:

W/System.err: java.net.ProtocolException: unexpected end of stream
W/System.err: at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close(HttpConnection.java:314) W/System.err: at com.android.okhttp.okio.RealBufferedSink.close(RealBufferedSink.java:241) W/System.err: at com.android.okhttp.okio.RealBufferedSink$1.close(RealBufferedSink.java:209) W/System.err: at java.io.FilterOutputStream.close(FilterOutputStream.java:67)
W/System.err: at com.mercadolibre.android.sdk.internal.HttpPut.performOperationBeforeConnect(HttpPut.java:52) W/System.err: at com.mercadolibre.android.sdk.internal.HttpOperation.execute(HttpOperation.java:100) W/System.err: at com.mercadolibre.android.sdk.internal.HttpOperation.execute(HttpOperation.java:74) W/System.err: at com.mercadolibre.android.sdk.Meli.put(Meli.java:534)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) W/System.err: at java.lang.Thread.run(Thread.java:818)

Upon retrying to send it, it would eventually work, somestimes taking up to 5 re-tries which suggest that the payload is not corrupt nor anything but an error on the connection.

Any idea on what might be causing it? I have reviewed the library and I call flush() and close() after writing so the Sink should not be having any size errors.Addtionally I have tried setting up a fixedsize for the connection to use the length of my payload, but nothing seems to work.

Thank you in advance.

Manuel G.
  • 61
  • 6

1 Answers1

2

This error occurs when your content-length disagees with the length of the data you actually write. How are you computing the content-length?

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Thank you for the answer. I am using payload.getBytes().length. And my connection has that set as fixed length. I have also attempted not setting a length, and writing my payload straight to the DataOutputWriter, but the same occurs. – Manuel G. Jun 10 '17 at 16:05
  • 1
    Try saving a reference to the `byte[]` returned from `payload.getBytes()` and use that `byte[]` when you write it to the stream. My guess is that the character set used by `payload.getBytes()` is not the same as the character set you’re using to write the body. When these two disagree you’ll get different lengths when the string has non-ASCII characters. – Jesse Wilson Jun 10 '17 at 21:20
  • Great observation and I will try that, but wouldn't it just fail every time since the payload content does not change, but it works after several retries? Thanks. – Manuel G. Jun 10 '17 at 23:17
  • I tried what you mentioned, and it worked. Thak you very much, if you can put it into the answer I will mark it as correct. And also if you could explain why the size is different each time it retries if the payload is again, wouldn't it encode it the same way every time? Thank you! – Manuel G. Jun 12 '17 at 02:14