1

I'm working on a project that was previously done by another developer. I continue to call API for the project. I've followed his way. But, I get stuck when I want to retrieve JsonArray and show it into RecyclerView. How I can to get the data from JSONArray.

Interface:

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)) 
}

Fragment:

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)
    }

}

Data Model:

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
}

the 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"
                    },
karan
  • 8,637
  • 3
  • 41
  • 78
Mavisa9
  • 144
  • 4
  • 14

1 Answers1

1

As I have seen, your data should be a JSONobject type value already and since you are using serialized name i assume you are using Gson library for it.

First, make another model beforehand to contain your List (since it passed in rows)

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

Then, you can try this

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

Your tripList will then be TripList type of class, access your list with tripList.myList. Hope it will work for your solution

DemiDust
  • 313
  • 1
  • 3
  • 19