Okhttp: ProtocolException: unexpected end of stream

Created on 21 Aug 2019  ·  5Comments  ·  Source: square/okhttp

Not 100% reproduce depend on device (100% failed on vivo X20A version: 8.1.0).

Repro code

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Timber.plant(Timber.DebugTree())
        net.setOnClickListener {
            Thread {
                Timber.w("network")
                val request = Request.Builder()
                        .url("http://45.63.13.41:3000/playlist/detail?id=2451836751")
                        .get()
                        .addHeader("Cache-Control", "no-cache")
                        .addHeader("Accept-Encoding", "gzip, deflate")
                        .build()

                val client = OkHttpClient()
                val response = client.newCall(request).execute()
                val body = response.body?.string()

                Timber.w("response = " + body)
            }.start()
        }
    }
}

Okhttp version: com.squareup.okhttp3:okhttp:4.1.0

StackTrace

E/AndroidRuntime: FATAL EXCEPTION: Thread-2
    Process: me.gengjiawen.quickstart, PID: 4865
    java.net.ProtocolException: unexpected end of stream
        at okhttp3.internal.http1.Http1ExchangeCodec$FixedLengthSource.read(Http1ExchangeCodec.kt:392)
        at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:279)
        at okio.Buffer.writeAll(Buffer.kt:1104)
        at okio.RealBufferedSource.readString(RealBufferedSource.kt:194)
        at okhttp3.ResponseBody.string(ResponseBody.kt:187)
        at me.gengjiawen.switchdemo.MainActivity$onCreate$1$1.run(MainActivity.kt:29)
        at java.lang.Thread.run(Thread.java:764)

Tried https://github.com/square/okhttp/issues/1490#issuecomment-78621872, but no luck.

Same code works on kotlin jvm.
The url also works on curl, browser, Node.js.

bug

All 5 comments

Looks like a server issue, though on mobile occurred very often.

Hi Jiawen ,

I have the exact same problem! I am curious to know what was the issue on the server side? Do you use node.js?

Thanks in advance,

Best regards,
Nicolas

yes, I am using Node.js.

I also use Node, so I think I am in the same case than you. How did you fix this? Was it a problem with the Content-Length header on the server side?

I have contacted the support of my PaaS provider and they told me that this could be related to their reverse proxy. They have deployed a fix on their infrastructure and the issue seems to be solved.

Was this page helpful?
0 / 5 - 0 ratings