简体   繁体   English

如何使用带有 RxJava 的 Retrofit GET 方法获得以下响应

[英]How to get below response using Retrofit GET method with RxJava

{"success":true,"rates": {"AT": {"country_name":"Austria","standard_rate":20,"reduced_rates": {"domestic flights":13,"entrance to sporting events":13,"admissions to cultural events and amusement parks":13,"firewood":13,"agricultural supplies":13}},"BE": {"country_name":"Belgium","standard_rate":21,"reduced_rates": {"some foodstuffs":12,"certain agricultural supplies":12,"social housing":12}}}}

I tried,我试过了,

@GET("rate_list")
fun getValidCountries(
    @Query("access_key") accessKey: String,
) : Single<Response<JsonElement>>

But getting error like,但是出现错误,例如,

Cannot construct instance of com.google.gson.JsonElement (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (okhttp3.ResponseBody$BomAwareReader);无法构造com.google.gson.JsonElement的实例(没有创建者,如默认构造,存在):抽象类型需要映射到具体类型,具有自定义反序列化器,或在 [Source: (okhttp3.ResponseBody $BomAwareReader); line: 1, column: 1]行:1,列:1]

How to solve this, also i'm using RxJava to call API.如何解决这个问题,我也在使用 RxJava 调用 API。

In your code swap在您的代码交换中

@GET("rate_list")
fun getValidCountries(@Query("access_key") accessKey: String) : Single<Response<JsonElement>>

With this有了这个

@GET("rate_list")
fun getValidCountries(@Query("access_key") accessKey: String) : Single<ValidCountries>

Use this to create data classes for your JSON.使用为您的 JSON 创建数据类。 And rename the class named Json4Kotlin_Base to ValidCountrie(s)并将名为Json4Kotlin_Base的 class 重命名为ValidCountrie(s)

ALso, you don't need Response use catch to catch any HttpException .此外,您不需要Response使用 catch 来捕获任何HttpException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM