How to get data from JsonObject and set as Array on RecyclerView in Kotlin?
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"
},

|
show 4 more comments
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"
},

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

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


edited Jan 2 at 8:24


Karan Mer
5,51132965
5,51132965
asked Jan 2 at 7:04
Mavisa9Mavisa9
219
219
What is the response of this api "driver/getAvailableTripList" ?
– Mahmoud Elshamy
Jan 2 at 7:22
have u tried to print out your data when you processed an json object type value
– DemiDust
Jan 2 at 7:41
yes, i print the data on Logcat @DemiDust
– Mavisa9
Jan 2 at 7:48
what is the log? it might help in finding your issue @Mavisa9
– DemiDust
Jan 2 at 8:14
the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust
– Mavisa9
Jan 2 at 8:24
|
show 4 more comments
What is the response of this api "driver/getAvailableTripList" ?
– Mahmoud Elshamy
Jan 2 at 7:22
have u tried to print out your data when you processed an json object type value
– DemiDust
Jan 2 at 7:41
yes, i print the data on Logcat @DemiDust
– Mavisa9
Jan 2 at 7:48
what is the log? it might help in finding your issue @Mavisa9
– DemiDust
Jan 2 at 8:14
the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust
– Mavisa9
Jan 2 at 8:24
What is the response of this api "driver/getAvailableTripList" ?
– Mahmoud Elshamy
Jan 2 at 7:22
What is the response of this api "driver/getAvailableTripList" ?
– Mahmoud Elshamy
Jan 2 at 7:22
have u tried to print out your data when you processed an json object type value
– DemiDust
Jan 2 at 7:41
have u tried to print out your data when you processed an json object type value
– DemiDust
Jan 2 at 7:41
yes, i print the data on Logcat @DemiDust
– Mavisa9
Jan 2 at 7:48
yes, i print the data on Logcat @DemiDust
– Mavisa9
Jan 2 at 7:48
what is the log? it might help in finding your issue @Mavisa9
– DemiDust
Jan 2 at 8:14
what is the log? it might help in finding your issue @Mavisa9
– DemiDust
Jan 2 at 8:14
the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust
– Mavisa9
Jan 2 at 8:24
the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust
– Mavisa9
Jan 2 at 8:24
|
show 4 more comments
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002477%2fhow-to-get-data-from-jsonobject-and-set-as-array-on-recyclerview-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Jan 3 at 3:27
DemiDustDemiDust
13911
13911
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002477%2fhow-to-get-data-from-jsonobject-and-set-as-array-on-recyclerview-in-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the response of this api "driver/getAvailableTripList" ?
– Mahmoud Elshamy
Jan 2 at 7:22
have u tried to print out your data when you processed an json object type value
– DemiDust
Jan 2 at 7:41
yes, i print the data on Logcat @DemiDust
– Mavisa9
Jan 2 at 7:48
what is the log? it might help in finding your issue @Mavisa9
– DemiDust
Jan 2 at 8:14
the log is the JsonArray of the api. i already my Question and put the data on it @DemiDust
– Mavisa9
Jan 2 at 8:24