1

I was trying to parse Json in array in array But It doesn't worked. Source was like this

    @POST("/storyGet")
fun getStory() : Call<ArrayList<StoryData>>

this is API.kt's and

Client.retrofitService.getStory().enqueue(object :
        retrofit2.Callback<ArrayList<StoryData>> {
        override fun onResponse(call: Call<ArrayList<StoryData>>?, response: Response<ArrayList<StoryData>>?) {
            val repo = response!!.body()
            when (response.code()) {
                200 -> {
                    repo!!.indices.forEach {
                        items += StoryDataSubListItem(
                                repo[it][it][it].alreadyWatch,
                                repo[it][it][it].createdAt,
                                repo[it][it][it].imgUrl,
                                repo[it][it][it].storyUUID,
                                repo[it][it][it].userName,
                                repo[it][it][it].userName,
                                repo[it][it][it].userUUID
                            )
                        recyclerView!!.adapter?.notifyDataSetChanged()
                    }
                }
            }
        }

        override fun onFailure(call: Call<ArrayList<StoryData>>?, t: Throwable?) {
        }
    })

This is my Retrofit Data and

class StoryData : ArrayList<StoryDataSubList>()

this is first Array and

class StoryDataSubList : ArrayList<StoryDataSubListItem>()

this is my second array and

data class StoryDataSubListItem(
val alreadyWatch: List<Any>,
val createdAt: String,
val imgUrl: String,
val storyUUID: String,
val userName: String,
val userProfileImgUrl: String,
val userUUID: String)

this is dataclass and json in array in array's format is

[
[
    {
        "alreadyWatch": [],
        "createdAt": "test",
        "_id": "_id",
        "userUUID": "userUUID2",
        "userName": "userName2",
        "userProfileImgUrl": "false",
        "imgUrl": "imageUrl",
        "storyUUID": "StoryUUID",
        "__v": 0
    }
],
[
    {
        "alreadyWatch": [],
        "createdAt": "test",
        "_id": "_id",
        "userUUID": "userUUID",
        "userName": "TEST NAME",
        "userProfileImgUrl": "false",
        "imgUrl": "imageURL",
        "storyUUID": "StoryUUID",
        "__v": 0
    }
]]

When I saw logcat, server was in normal operation when like this how should I fix It? please help and thank you in advance.

2
  • Is response.body() not null? check it and where you set list to the adapter? Commented Jul 1, 2020 at 14:16
  • when I see logcat when response code is 200 it always return json Commented Jul 1, 2020 at 14:22

2 Answers 2

1

You need to change your code as below, since the response has list of another list of the given model data,

 @POST("/storyGet")
 fun getStory() : Call<List<List<StoryDataSubList>>>

Full code is as below

@POST("/storyGet")
     fun getStory() : Call<List<List<StoryDataSubList>>>

method calling retrofit API to get the response as below

private fun loadStoryData() {
            val call = netWorkApi.getStory() // replace netWorkApi with your retrofit API sevice
    
            call.enqueue(object : Callback<List<List<StoryDataSubList>>> {
                override fun onResponse(
                    call: Call<List<List<StoryDataSubList>>>,
                    response: Response<List<List<StoryDataSubList>>>
                ) {
                    if (response.isSuccessful) {
                        for (index in response.body()?.indices!!)
                            Log.d("TAG", "${response.body()!!.get(index)}")
    
                    }
                }
    
                override fun onFailure(call: Call<List<List<StoryDataSubList>>>, t: Throwable) {
                    TODO("Not yet implemented")
                }
            });
    
        }
Sign up to request clarification or add additional context in comments.

2 Comments

sorry when i copied this source and insert but it dosen't worked server was worked normal operation and it return json...
Oh When I changed StoryDatSubList To data class it worked! Thank You!!!
0

Replace ArrayList with List and try it.

3 Comments

abstract class StoryData : List<StoryDataSubList> like this I Changed all ArrayList To List but it was not worked
abstract?!! You should use data class for all model class.
when 200 ok response is come and noting is happend even no errors..

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.