Home > Mobile >  I have a JSON package and I need to display the data in it in my application, how can I do that?
I have a JSON package and I need to display the data in it in my application, how can I do that?

Time:12-25

I am making an application about solar panels and I need to pull the status and efficiency of solar panels from the database with the help of API. I found a site, but I couldn't find the API file in it, it only offers JSON packages specific to the region I selected. How can I pull data with JSON package?

CodePudding user response:

u need to learn how to use JSON and Okhttp in kotlin

first add dependencies of okhttp to ur project

then create a object from OKHTTPCLIENT() like this:

val client = OKHTTPCLIENT()

val request = Request.Builder()
.url("URL OF JSON")
.build()

next add this following codes at onResponse fun

val rawContent = response.body!!.string()
val jsonObject = JSONOBJECT(rawContent)
jsonObject.getInt("ID OF content u want")

now u have a jsonObject here

JSON Doc here : https://developer.android.com/reference/kotlin/org/json/JSONObject

  • Related