You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Compose+Retrofit提交症状列表时应用崩溃求助

Android Compose应用点击预测按钮崩溃求助

问题描述

我开发了一款基于Android Compose的疾病预测应用:用户选择症状列表后,点击预测按钮通过Retrofit发送POST请求给ML模型API,返回预测的疾病类型。目前症状选择功能完全正常,但点击预测按钮时应用直接崩溃,无法完成请求流程。

关键代码片段

1. Retrofit API接口定义

interface DiseasePredictionApi {
    @POST("/predict")
    suspend fun predictDisease(@Body selectedSymptoms: List<String>): Response<DiseasePredictionResult>
}

data class DiseasePredictionResult(
    val diseaseName: String,
    val confidenceScore: Double
)

2. Compose预测按钮实现

@Composable
fun PredictionActionButton(
    selectedSymptoms: List<String>,
    predictionApi: DiseasePredictionApi,
    onResult: (DiseasePredictionResult?) -> Unit
) {
    Button(
        onClick = {
            // 点击触发请求逻辑
            try {
                val response = predictionApi.predictDisease(selectedSymptoms)
                if (response.isSuccessful) {
                    onResult(response.body())
                }
            } catch (e: Exception) {
                e.printStackTrace()
                onResult(null)
            }
        },
        enabled = selectedSymptoms.isNotEmpty()
    ) {
        Text("预测疾病")
    }
}

3. 模块级Gradle依赖配置

dependencies {
    // Compose核心依赖
    implementation "androidx.compose.ui:ui:1.5.4"
    implementation "androidx.compose.material3:material3:1.1.2"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.2"
    
    // Retrofit与解析器
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    
    // Kotlin协程
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
}

崩溃日志截图

Logcat崩溃详情

请求协助

请帮忙排查崩溃原因并给出修复方案,比如是否是Retrofit实例配置错误、协程线程使用不当、API请求格式不匹配,或是其他潜在问题?


内容的提问来源于stack exchange,提问作者Anirudh parida

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 12:57:11