0

So here is my problem i try to parse a json array of object but an error is thrown. I created a groupListType var because Gson did not parsing me my objects and let me with an array of String and this gave me the error

here is the code of the function :

fun getPromoCode(): Array<Code> {


        val urlBuilder = HttpUrl.Builder()
            .scheme("https")
            .host("dev.api.gostyle.ovh")
            .addPathSegment("api")
            .addPathSegment("promotion")
            .addPathSegment("code")
            .addQueryParameter("user_id",sharedPreference.getValueString("UserId"))
            .build()


        var request = Request.Builder()
            .header("Authorization",sharedPreference.getValueString("MyGoStyleToken"))
            .url(urlBuilder)
            .build()


        var mareponse = client.newCall(request).execute()

            if (!mareponse.isSuccessful) throw IOException("Unexpected code $mareponse")

            var a: String = mareponse.body?.string().toString()
            val groupListType: Type = object: TypeToken<ArrayList<Code>>() {}.type
            var test = Gson().fromJson<Array<Code>>(a,groupListType) //Error here

            return test
    } 

My DataClass :

data class Code(
    var id: String = "default",
    var code:String = "default",
    var date_start:String = "default",
    var date_end:String = "default",
    var description: String = "default",
    var reduction: Double = 0.0
)

the error log :

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.example.mygostyle, PID: 13854
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.example.mygostyle.Code[]
        at com.example.mygostyle.OkHttpHelper.getPromoCode(OkHttpHelper.kt:95)
        at com.example.mygostyle.ui.home.HomeFragment$MyCodesAdapter.<init>(HomeFragment.kt:72)
        at com.example.mygostyle.ui.home.HomeFragment$onCreateView$2.run(HomeFragment.kt:40)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

exemple of json object:

[
    {
        "id": "77fa026a-d519-40ec-ad03-a0532036473f",
        "code": "TEST1",
        "date_start": "2020-02-10T00:00:00.000Z",
        "date_end": "2020-02-10T00:00:00.000Z",
        "description": "TEST1",
        "reduction": 20
    },
    {
        "id": "8bade40a-33e5-4eee-a093-6b778c89df32",
        "code": "TEST2",
        "date_start": "2020-02-10T00:00:00.000Z",
        "date_end": "2020-02-10T00:00:00.000Z",
        "description": "TEST2",
        "reduction": 30
    }
]

1 Answers1

0

Array and ArrayList are different types. All you need is this:

val result: Array<Code> = Gson().fromJson(a, Array<Code>::javaClass)
// use `result.toList()` if you need a list instead of an array

Extra links:

Nikolay Kulachenko
  • 4,604
  • 4
  • 31
  • 37