如何从JsonObject获取数据并在Kotlin的RecyclerView上设置为Array?

时间:2019-01-02 07:04:24

标签: android kotlin

我正在做一个以前由另一个开发人员完成的项目。我继续为该项目调用API。我一直遵循他的方式。但是,当我想检索JsonArray并将其显示到RecyclerView中时,我陷入了困境。 如何从JSONArray获取数据。

接口:

interface TripService {
    @FormUrlEncoded
    @POST("driver/getAvailableTripList")
    fun availableTripList(@Field("page") page : String): Call<JsonObject>
}

TripServiceHandler:

class TripServiceHandler {
    private var instance: TripService? = null
    // Singleton instance to access through the application

    init {
        if(instance == null) {
            instance = TripFactory().createJourney(TripService::class.java)
        }
    }


    // Method for Driver Balance Service
    fun availableTripList(page: String, listener: ServerCallbackListener) =
            this.instance?.let {this.instance!!.availableTripList(page).enqueue(RetrofitCallBackHandler.getHandler(listener)) 
}

片段:

private fun getAvailableTrip(){
    showLoading()
    TripServiceHandler().availableTripList("1", availableTripHandler)
}

private var availableTripHandler: ServerCallbackListener = object : ServerCallbackListener {
    override fun onSuccess(baseResponse: JsonObject?) {
        hideLoading()
        val data = baseResponse!!.getAsJsonObject(AppConstants.KEY_DATA)

    }

    override fun onFailure(message: String?) {
        showMessageBar(message, SnackBarUtility.MessageType.ERROR)
    }

}

数据模型:

class Trip : Serializable {

    @SerializedName("tid")
    var tripId: String? = null

    @SerializedName("max_passengers")
    var maxPass: String? = null

    @SerializedName("driver_revenue")
    var priceTrip: String? = null

    @SerializedName("origin_coordinate")
    var originCoordinate: List<Coordinate>? = null

    @SerializedName("destination_coordinate")
    var destCoordinate: List<Coordinate>? = null
}

jsonArray

"rows": [
            {
                "tid": "44551",
                "did": null,
                "status": 1,
                "leaving_time": "1547093186",
                "max_passengers": 12,
                "total_revenue": 0,
                "driver_revenue": 0,
                "origin_coordinate": {
                    "x": 1.43762623,
                    "y": 103.80153311
                },
                "destination_coordinate": {
                    "x": 1.29481854,
                    "y": 103.78735487
                },
                "total_requests": 12,
                "destination_geom": "0101000020E610000048F0284063F25940854F5F1901BFF43F",
                "origin_geom": "0101000020E61000002AB6CC40C7F25940032A19ECF7E4F63F",
                "stops": [
                    {
                        "sid": "46969",
                        "count": "4",
                        "order_seq": 1,
                        "mode": 0,
                        "name": "Woodlands Ave 4",
                        "description": "Blk 532",
                        "coordinate": {
                            "x": 1.43059181782,
                            "y": 103.792699721
                        },
                        "eta_time": "1547093186",
                        "leaving_time": "1547093366"
                    },

1 个答案:

答案 0 :(得分:0)

正如我所见,您的数据应该已经是一个JSONobject类型值,并且由于您使用的是序列化名称,所以我假设您正在使用Gson库。

首先,预先制作另一个模型来包含您的列表(因为它是通过行传递的)

data class TripList:Serializeable{
    @("rows")
    myList:List<Trip> = ArrayList()
}

然后,您可以尝试

val tripList = gson.fromJson(data, TripList::class.java)

您的tripList将成为TripList类型的类,使用tripList.myList访问您的列表。希望它能为您解决方案