2

I have a get request that returns certain features where 11 properties of the objects are common. Since data classes in Kotlin can't inherit from other classes i decided to define the common properties with the following interface.

interface AccountFeature {
    val feature: String?
    val status: String?
    val id: String?
    val urlLogo: String?
    val minAppVersion: String?
    val target: String?
    val title: String?
    val backgroundColor: String?
    val bodyTextColor: String?
    val bodyText: String?
    val titleTextColor: String?
}

One of the data classes look like this.

data class AccountFeatureHelp(val privacyStatement: String? = null,
                              val supportFAQ: String? = null,
                              val termsOfService: String? = null,
                              val supportHotline: String? = null,
                              val supportEmail: String? = null,
                              override val feature: String?,
                              override val status: String?,
                              override val id: String?,
                              override val urlLogo: String?,
                              override val minAppVersion: String?,
                              override val target: String?,
                              override val title: String?,
                              override val backgroundColor: String?,
                              override val bodyTextColor: String?,
                              override val bodyText: String?,
                              override val titleTextColor: String?): AccountFeature

So far everthing is fine.

In the Interface definition of my Request, i expect an observable array of AccountFeature. My goal is depending on the feature value, mapping the hashmap to corresponding Feature object.

interface  AccountFeaturesAPIService {
    @GET("accounts/{id}/features")
    fun getAccountFeature(@Path("id") id: String): Observable<Array<AccountFeature>>
}

I get the following runtime exception.

java.lang.RuntimeException: Unable to invoke no-args constructor for interface com.thinxnet.native_tanktaler_android.core.model.account.feature.AccountFeature. Registering an InstanceCreator with Gson for this type may fix this problem.

How would i overcome this in an elegant way apart from changing Observable> to Observable>

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79
  • can you check your api what its trying to send you because i think there different data types your getting – Ashish Jan 10 '19 at 12:59
  • what do you mean – Ilker Baltaci Jan 10 '19 at 13:00
  • some type you get output similar to this AccountFeature.feature cause https://stackoverflow.com/a/30372271/10182897 check this – Ashish Jan 10 '19 at 13:00
  • O I see, the response is fine actually. – Ilker Baltaci Jan 10 '19 at 13:04
  • Do you really have a JSONArray as response, not JSONObject? If not, you might try `fun getAccountFeature(@Path("id") id: String): Observable` with `data class AccountFeatureResponse(@SerializedName("accountFeatures") val accountFeatures: List?)` – Onik Jan 10 '19 at 21:13

0 Answers0