Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected BEGIN_OBJECT but was BEGIN_ARRAY kotlin

Tags:

kotlin

I'm trying to parse a JSON string data used gson and okhttp in my app . l used recycle view for displaying data . when l run app l got FATAL EXCEPTION

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.iraqairoirt.iraqairports, PID: 20692
    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
        at com.google.gson.Gson.fromJson(Gson.java:927)
        at com.google.gson.Gson.fromJson(Gson.java:892)
        at com.google.gson.Gson.fromJson(Gson.java:841)
        at com.google.gson.Gson.fromJson(Gson.java:813)
        at com.iraqairoirt.iraqairports.NotamOrbi$fetchjson$1.onResponse(NotamOrbi.kt:55)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)
     Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
        at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter

my main activity

class NotamOrbi : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.notam_orbi)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerDate)
        recyclerView.layoutManager = LinearLayoutManager(this)

        fetchjson()

    }

    fun fetchjson() {

        val url =
            "/&locations=orbi"
        val request = Request.Builder().url(url).build()
        val client = OkHttpClient()
        client.newCall(request).enqueue(object : Callback {

            override fun onResponse(call: Call, response: Response) {
                val body = response?.body()?.string()
                println(body)

                val gson = GsonBuilder().create()

                val homedata = gson.fromJson(body, HomeDate::class.java)

                runOnUiThread{
                    recyclerDate.adapter=NotamOrbiAdapter(homedata)

                }

            }

            override fun onFailure(call: Call, e: IOException) {

            }


        })


    }

    class HomeDate( val datanotam:ArrayList<NotamORBI>)
    class NotamORBI(val id : String)

    class NotamOrbiAdapter (val datajson:HomeDate): RecyclerView.Adapter<NotamOrbiAdapter.ViewHolder>() {
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

            val v = LayoutInflater.from(p0.context).inflate(R.layout.notam_reclycer_card, p0, false)
            return ViewHolder(v)
        }

        override fun getItemCount(): Int {
            return datajson.datanotam.count()
        }

        override fun onBindViewHolder(p0: ViewHolder, p1: Int) {

           val data = datajson.datanotam.get(p1)
            p0?.itemView.id_notam.text=data.id

        }

        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            val id_notam = itemView.findViewById<TextView>(R.id.id_notam)
        }
    }
}

the problem is in line 55 val homedata = gson.fromJson(body, HomeDate::class.java)

data json url

 [
      {
        "_id": "5c0b4ed2ab233e849941925c",
        "id": "A0466/18",
        "entity": "MR",
        "status": "LC",
        "Qcode": "MRLC",
        "Area": "AGA",
        "SubArea": "Movement and landing area",
        "Condition": "Limitations",
        "Subject": "Runway",
        "Modifier": "Closed",
        "message": "RWY 15L/33R CLSD DUE TO MAINT DURING VMC ONLY.\nCREATED: 05 Dec 2018 06:59:00 \nSOURCE: ORBIYNYX",
        "startdate": "2018-12-07T11:00:00.000Z",
        "enddate": "2018-12-17T13:00:00.000Z",
        "all": "A0466/18 NOTAMN\nQ) ORBB/QMRLC/IV/NBO/A/000/999/3316N04414E005\nA) ORBI\nB) 1812071100\nC) 1812171300\nD) MON FRI 1100-1300\nE) RWY 15L/33R CLSD DUE TO MAINT DURING VMC ONLY.\nCREATED: 05 Dec 2018 06:59:00 \nSOURCE: ORBIYNYX",
        "location": "ORBI",
        "isICAO": true,
        "Created": "2018-12-05T06:59:00.000Z",
        "key": "A0466/18-ORBI",
        "type": "airport",
        "StateCode": "IRQ",
        "StateName": "Iraq"
      }
    ]

l kow the problem is with json array but l dont know how to fix l am new in gson . Any ideas how should I fix it?

like image 654
Ali Ghassan Avatar asked Jan 02 '23 13:01

Ali Ghassan


1 Answers

Transform it into a list first, then you can iterate through that list.

val homedateList: List<HomeDate> = gson.fromJson(body, Array<HomeDate>::class.java).toList()
like image 69
Dr4ke the b4dass Avatar answered Jan 13 '23 12:01

Dr4ke the b4dass