[英]Retrofit Parameter specified as non-null is null
我是Kotlin的新手,正在嘗試解析一個簡單的JSON,但我收到一個" Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter vouchers"
result.products
始終為null,但我可以在日志中看到,經過200 ok的請求,改造已正確獲取json。 所以我想當我嘗試解析json時這可能是個問題
我該如何解決?
我在下面添加了我的代碼
disposable = ApiServe.getVouchers()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result -> processVouchers(result.products) },
{ error -> error(error.message)}
)
fun processVouchers(vouchers : List<Product>){
mCallback?.onResponseVouchers(vouchers)
}
在ApiServe類中獲取VOUCHERES
@GET(Api.ENDPOINT.VOUCHER_ENDPOIN)
fun getVoucher(): Observable<Response<Vouchers>>
模型
data class Voucher(val products: List<Product>)
data class Product(val code: String, val name: String, val price: Double)
JSON
{"products":[{"code":"Voucher","name":"Voucher","price":3},{"code":"Ball","name":"Voucher Ball","price":10},{"code":"Milk","name":"Voucher Milk","price":8.5}]}
我認為這是因為您要在Retrofit
服務界面中使用Response
包裝返回類型。 只是嘗試這樣更改:
@GET(Api.ENDPOINT.VOUCHER_ENDPOIN)
fun getVoucher(): Observable<Voucher>
我認為這里的問題可能是
fun getVouchers(): Observable<Voucher.Vouchers>
您確定getVoucher返回正確的類型嗎? 它不是Observable<Voucher>
嗎?
編輯:事實證明,作者正在為其GsonConverterFactory使用excludeFieldsWithModifiers
,這導致了模型解析的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.