Android 二,;具有多个可能值的JSON响应数组
我很久没有在这里写任何东西了,但现在我真的需要建议。) 我将改型2用作api客户端。服务器API有一个端点,例如,Android 二,;具有多个可能值的JSON响应数组,android,kotlin,retrofit2,Android,Kotlin,Retrofit2,我很久没有在这里写任何东西了,但现在我真的需要建议。) 我将改型2用作api客户端。服务器API有一个端点,例如,/API/stats,它接收JSON body请求,并返回JSON响应,如下所示: data class StatsResult<T>( @SerializedName("code") val code: Int, @SerializedName("message") val msg: St
/API/stats
,它接收JSON body请求,并返回JSON响应,如下所示:
data class StatsResult<T>(
@SerializedName("code") val code: Int,
@SerializedName("message") val msg: String?,
@SerializedName("request_id") val requestId: String?,
@SerializedName("data") val data: T?
)
请求2:
{
"type": "type2",
"params": {
}
}
Response:
{
"code": 0,
"request_id": "...",
"data": [
{
"key3": "value1",
"key4": "value2"
},
{
"key3": "value3",
"key4": "value4"
}
]
}
简而言之,以下是我的实现:
interface StatsApi {
@POST("/api/stats")
suspend fun getStats(@Body request: StatsRequest): ApiResponse<StatsData>
}
sealed class ApiResponse<out T: Any> {
data class Success<T: Any>(val body: T): ApiResponse<T>()
object Unauthorized : ApiResponse<Nothing>()
object Forbidden: ApiResponse<Nothing>()
object NetworkError: ApiResponse<Nothing>()
data class Error(val msg: String? = null): ApiResponse<Nothing>()
data class Exception(val t: Throwable): ApiResponse<Nothing>()
}
typealias StatsData = StatsResult<List<BaseStatsDto>>
open class BaseStatsDto()
class Type1StatsDto: BaseStatsDto() {
@SerializedName("key1") var key1: String? = null
@SerializedName("key2") var key2: String? = null
}
class Type2StatsDto: BaseStatsDto() {
@SerializedName("key3") var key3: String? = null
@SerializedName("key4") var key4: String? = null
}
我看到了另一种解决方案——使用不同响应类型的独立接口函数。而且工作正常:
@POST("/api/stats")
suspend fun getType1Stats(@Body request: StatsRequest): ApiResponse<StatsResult<List<Type1StatsDto>>>
@POST("/api/stats")
suspend fun getType2Stats(@Body request: StatsRequest): ApiResponse<StatsResult<List<Type2StatsDto>>>
@POST(“/api/stats”)
suspend fun getType1Stats(@Body request:StatsRequest):ApiResponse
@POST(“/api/stats”)
suspend fun getType2Stats(@Body request:StatsRequest):ApiResponse
但是,如果统计数据类型计数增加,那么维护它将非常不舒服
我想要一个统计api端点。使用Set-SourceType:JSON和Annonation-style:Gson
请求1的数据
class Data1 {
@SerializedName("key1")
@Expose
var key1: String? = null
@SerializedName("key2")
@Expose
var key2: String? = null
}
请求1的响应
class Response1 {
@SerializedName("code")
@Expose
var code: Int? = null
@SerializedName("request_id")
@Expose
var requestId: String? = null
@SerializedName("data")
@Expose
var data: List<Data1>? = null
}
对请求2的答复
class Data2 {
@SerializedName("key3")
@Expose
var key3: String? = null
@SerializedName("key4")
@Expose
var key4: String? = null
}
class Response1 {
@SerializedName("code")
@Expose
var code: Int? = null
@SerializedName("request_id")
@Expose
var requestId: String? = null
@SerializedName("data")
@Expose
var data: List<Data2>? = null
}
类响应1{
@序列化名称(“代码”)
@暴露
变量代码:Int?=null
@SerializedName(“请求id”)
@暴露
var requestId:字符串?=null
@SerializedName(“数据”)
@暴露
变量数据:列表?=null
}
与
@POST(“/api/stats”)
suspend fun getStats1(@Body request:StatsRequest1):ApiResponse
@POST(“/api/stats”)
suspend fun getStats2(@Body request:StatRequest2):ApiResponse
谢谢您的回复!正如我在问题末尾所写的,这个解决方案是可行的,但我希望所有统计数据都有一个api端点
class Data2 {
@SerializedName("key3")
@Expose
var key3: String? = null
@SerializedName("key4")
@Expose
var key4: String? = null
}
class Response1 {
@SerializedName("code")
@Expose
var code: Int? = null
@SerializedName("request_id")
@Expose
var requestId: String? = null
@SerializedName("data")
@Expose
var data: List<Data2>? = null
}
@POST("/api/stats")
suspend fun getStats1(@Body request: StatsRequest1): ApiResponse<Response1>
@POST("/api/stats")
suspend fun getStats2(@Body request: StatsRequest2): ApiResponse<Response2>