当参数传递给接口(interface)时,Json Server数据在Android中使用Retrofit2和rxjava2显示,然后给出错误。

class Sales : AppCompatActivity() {

    internal lateinit var api : APIInterface

    private var compositeDisposable : CompositeDisposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sales)


        compositeDisposable = CompositeDisposable()

       fetchYearData()
    }

  private fun fetchYearData(){

        val retrofit = APIClient.apIClient
        if (retrofit != null) {
            api = retrofit.create(APIInterface::class.java)
        }


   compositeDisposable!!.add(api.getSalesGSTData(1,1,"04/01/2018","31/03/2019")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( { displaySalesGSTData(it.salesGST) },
                {

                    Toast.makeText(this,it.message,Toast.LENGTH_LONG).show()


        }))



    }

成功后以及在JSONArray和JSONObject之后,模型类SalesGSTList JsonObject。

SalesGSTList.kt
class SalesGSTList {

    val success : String = ""

    lateinit var salesGST : ArrayList<SalesGST>
}

SalesGST.kt
class SalesGST {

    var FYearID : Int = 0

    var Cmp_Name : String? = ""

    var GSTIN : String? = ""
}

接口(interface)
@GET("SalesGST.php")
    fun getSalesGSTData(@Query("cid") cid : Int,@Query("fid") fid : Int,@Query("fd") fd : String,@Query("td") td : String) : Observable<SalesGSTList>

最佳答案

只需从lateinit中的lateinit var salesGST : ArrayList<SalesGST>行中删除SalesGSTList即可。

class SalesGSTList {

    val success : String = ""

    var salesGST : ArrayList<SalesGST>? = null
    // you can also use
    // var salesGST : ArrayList<SalesGST>  = ArrayList()
}

有关更多信息,请访问kotlin-when-to-use-lazy-or-lateinit

10-08 18:00