androidkotlinretrofitkotlin-coroutines

How to solve error "Channel is unrecoverably broken and will be disposed!"


After clicking the button I am getting these errors:

Channel is unrecoverably broken and will be disposed!

and

Failed to open APK '/data/app...

and

failed to add asset path /data/app

Here is the code from the MainActivity:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val tv = findViewById<TextView>(R.id.tv)
        val button = findViewById<Button>(R.id.button)

        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY

        val client = OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .build()

        val retrofit = Retrofit.Builder()
            .baseUrl("https://dummyjson.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val productApi = retrofit.create(ApiService::class.java)

        button.setOnClickListener {
            CoroutineScope(Dispatchers.IO).launch {
                val product = productApi.getProductById()
                runOnUiThread {
                    tv.text = product.title
                }
            }
        }
    }
}

ApiService:

interface ApiService {
    @GET("products/1")
    suspend fun getProductById(): Product
}

Product:

data class Product(
    val id: Int,
    val title: String,
    val description: String,
    val price: Int,
    val discountPercentage: Float,
    val rating: Int,
    val stock: Float,
    val brand: String,
    val category: String,
    val thumbnail: String,
    val images: List<String>,
)

I tried to Clean and Rebuild Project, Invalidate cashes and Restart and turned off MIUI optimisation on phone settings.


Solution

  • Problem solved. I just edited Product class. Instead of val rating: Int I used val rating: Float, so now I get response from API correctly.