当前位置: 首页 > 知识库问答 >
问题:

onRetrofitFailure()无法将`java.util.ArrayList`的实例反序列化出START_OBJECT令牌

壤驷麒
2023-03-14
@Parcelize
@JsonIgnoreProperties(ignoreUnknown = true)
open class MerchantPromotionDTO(

    @JsonProperty("image") var image: ImageUrlsDTO? = null,
    @JsonProperty("cta") var cta: CTADTO? = null,
    @JsonProperty("probability") var probability: Int? = 0,
    @JsonProperty("isDismissible") var isDismissible: Boolean? = true,
    @JsonProperty("showImmediate") var showImmediate: Boolean? = false
    ) : BaseResponseDTO()

@Parcelize
    @JsonIgnoreProperties(ignoreUnknown = true)
    class MerchantpromotionBDTO(
    @JsonProperty("promotions") var promotions: ArrayList<MerchantPromotionDTO>?=null
) : BaseResponseDTO()

@GET("https://demo6861386.mockable.io/banner/test")
fun getPromotionalBanner(): Call<ArrayList<MerchantpromotionBDTO>>

fun getPromotionalDetails(): LiveData<ResponseDTO<ArrayList<MerchantpromotionBDTO>>>{
    val pBannerDetails=MutableLiveData<ResponseDTO<ArrayList<MerchantpromotionBDTO>>>()
    ApiComponent.enqueue({
        getPromotionalBanner(

        )
    },object :OnRequestComplete<ArrayList<MerchantpromotionBDTO>>{
        override fun onComplete(responseDTO: ResponseDTO<ArrayList<MerchantpromotionBDTO>>) {
            pBannerDetails.value=responseDTO
        }
    }
)

编辑:

override fun onStart() {
    super.onStart()

    populateData()
}

@Synchronized
private fun populateData() {       
    MerchantpromotionBDTO?.let {
        val promImageUrl = it.image?.getImageUrl(CommonUtils.getDisplayDensityLevel(context))
        picasso.load(if (promImageUrl.isNullOrEmpty()) null else promImageUrl)
                .placeholder(R.drawable.ic_placeholder_minimal)
                .into(ivMerchantPromotionBanner)
    }
}

我希望响应到我的logcat中,并在Imageview中获取图像

共有1个答案

赵飞雨
2023-03-14

问题在于,从后端获得的JSON是:

{
    "promotions": [
        ...
    ]
}

Thas是一个JSON对象,而不是数组,但是您将GetPromotionalBanner方法定义为返回列表 ,因此JSON库不能执行反序列化,因为它需要一个数组(即以[]开头的东西),但它会找到一个START_OBJECT标记(即{)。

可以通过将该方法的签名更改为:

@GET("https://demo6861386.mockable.io/banner/test")
fun getPromotionalBanner(): Call<MerchantpromotionBDTO>
 类似资料: