retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

Broken pipe Exception with POST call

Open EverglowingComet opened this issue 4 years ago • 1 comments

I am having broken pipe exception with calling my api I hope you to help me with this.

RegisterApi.kt


interface RegisterApi {

    @POST("/device")
    suspend fun registerDevice(
        @Body body: RegisterBody
    ) : Response<RegisterDeviceResponse>

    class RegisterDeviceResponse (
        val accessToken: String?
    )

    companion object {
        private var INSTANCE: RegisterApi? = null

        fun getInstance(): RegisterApi {
            return INSTANCE?: synchronized(this) {
                val retrofit = Retrofit.Builder()
                    .baseUrl(ZoomieConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                val instance = retrofit.create(RegisterApi::class.java)

                INSTANCE = instance

                instance
            }
        }
    }
}

RegisterBody.kt

class RegisterBody (
    @SerializedName("appVersion")
    val appVer: String?,
    @SerializedName("deviceType")
    val devType: String?,
    @SerializedName("deviceUDID")
    val devUDID: String?,
    @SerializedName("osVersion")
    val osVer: String?,
    @SerializedName("pushToken")
    val push: String?,
    @SerializedName("timeZone")
    val timeZone: String?,
    @SerializedName("userId")
    val userId: String?,
)

MyRepository.kt

class MyRepository() {
    val registerApi: RegisterApi = RegisterApi.getInstance()

    suspend fun registerDevice() = withContext(Dispatchers.IO) {
            val response = registerApi.registerDevice(
                RegisterBody(
                    "1.0.2x",
                    "IOS",
                    "random_device_generated_udid",
                    "14",
                    "random_push_token_updated",
                    "EST",
                    ""
                )
            )

            val token = response.body()?.accessToken

    }

}

MyApplication.kt

class MyApplication : Application() {
    val applicationScope = CoroutineScope(SupervisorJob())

    val userRepository: MyRepository by lazy { MyRepository() }

    override fun onCreate() {
        super.onCreate()
        applicationScope.launch {
            userRepository.registerDevice()
        }
    }
}

And this returns Exception as follows

    Process: com.zoomie.android, PID: 30537
    java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:117)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:161)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLOutputStream.writeToSocket(ConscryptEngineSocket.java:715)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLOutputStream.writeInternal(ConscryptEngineSocket.java:689)
        at com.android.org.conscrypt.ConscryptEngineSocket$SSLOutputStream.write(ConscryptEngineSocket.java:652)
        at okio.OutputStreamSink.write(JvmOkio.kt:53)
        at okio.AsyncTimeout$sink$1.write(AsyncTimeout.kt:103)
        at okio.RealBufferedSink.flush(RealBufferedSink.kt:267)
        at okhttp3.internal.http2.Http2Writer.windowUpdate(Http2Writer.kt:253)
        at okhttp3.internal.http2.Http2Connection.start(Http2Connection.kt:501)
        at okhttp3.internal.http2.Http2Connection.start$default(Http2Connection.kt:495)
        at okhttp3.internal.connection.RealConnection.startHttp2(RealConnection.kt:358)
        at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.kt:341)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:209)
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

It produces exception on MyRepository's registerApi.registerDevice call. I can not proceed with response I hope you guys help me out to take care of this issue

Thank you.

EverglowingComet avatar Oct 16 '21 01:10 EverglowingComet

most usually, writing to a connection when the other end has already closed it; less usually, the peer closing the connection without reading all the data that is already pending at his end. So in both cases you have a poorly defined or implemented application protocol.

There is a third reason which I will not document here but which involves the peer taking deliberate action to reset rather than properly close the connection

Amir-yazdanmanesh avatar Jan 04 '22 18:01 Amir-yazdanmanesh

This is a "normal" failure (somewhat of an oxymoron) of an HTTP socket, and it's one that happens at a layer well below anything we control. No action to take on Retrofit's part.

JakeWharton avatar Jan 26 '24 03:01 JakeWharton