单击按钮后,我收到以下错误:
通道已不可恢复损坏,将被处置!
和
无法打开 APK '/data/app...
和
添加资产路径/data/app失败
这是 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
}
}
}
}
}
API服务:
interface ApiService {
@GET("products/1")
suspend fun getProductById(): 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>,
)
我尝试清理并重建项目、无效现金并重新启动,并在手机设置中关闭 MIUI 优化。
问题解决了。我刚刚编辑了产品类别。而不是“val rating: Int”,“val rating: Float”,所以现在我正确地从 API 得到响应。