5

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?

3
  • The json you're receiving is an array, while your code is expecting an object. Json array look likey [...] while object {...}. Commented Dec 8, 2018 at 18:42
  • any solution please ? Commented Dec 8, 2018 at 18:46
  • You might find this link useful. futurestud.io/tutorials/… Commented Dec 8, 2018 at 18:49

1 Answer 1

10

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()
Sign up to request clarification or add additional context in comments.

4 Comments

your code is work , thanks but l got only the text view for activity not the data from json
sorry, I don't understand the question
l mean l didn't get the information inside data json . l got empty data in card view
You must iterate through the List to get all the data. Now you have a list of object. To iterate through list, for (element in List) {...}

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.