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"
                    },
9
  • What is the response of this api "driver/getAvailableTripList" ? Commented Jan 2, 2019 at 7:22
  • have u tried to print out your data when you processed an json object type value Commented Jan 2, 2019 at 7:41
  • yes, i print the data on Logcat @DemiDust Commented Jan 2, 2019 at 7:48
  • what is the log? it might help in finding your issue @Mavisa9 Commented Jan 2, 2019 at 8:14
  • the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust Commented Jan 2, 2019 at 8:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.