【问题标题】:How to get data from JsonObject and set as Array on RecyclerView in Kotlin?如何从 JsonObject 获取数据并在 Kotlin 的 RecyclerView 上设置为数组?
【发布时间】:2019-05-28 20:57:21
【问题描述】:

我正在处理一个以前由其他开发人员完成的项目。我继续为该项目调用 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
}

json数组

"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"
                    },

【问题讨论】:

  • 这个 api "driver/getAvailableTripList" 的响应是什么?
  • 您在处理 json 对象类型值时是否尝试过打印数据
  • 是的,我在 Logcat @DemiDust 上打印数据
  • 什么是日志?它可能有助于找到您的问题@Mavisa9
  • 日志是api的JsonArray。我已经是我的问题并将数据放在上面@DemiDust

标签: android kotlin


【解决方案1】:

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

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

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

那你可以试试这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2019-09-24
    • 2019-02-11
    • 1970-01-01
    • 2022-12-19
    • 2021-06-28
    相关资源
    最近更新 更多