0

I managed to make API call in my activity and it does show me API results in logcat but I can't figure out how to display those data in my view?

Sample data

Note: data are under data array.

{
  "data": [
    {
      "id": "0ade1bfb-6d02-4a1f-9cd4-dc88fa8aadbd",
      "name": "a",
      "photo": null
    },
    {
      "id": "30a18b38-c563-4b3a-8c7d-af483c616e49",
      "name": "b",
      "photo": null
    },
    {
      "id": "709804f8-e429-4c9d-b5cd-2cf3b2add98c",
      "name": "c",
      "photo": null
    },
    {
      "id": "be204e7b-f1ab-45bc-a842-3d5f3f033381",
      "name": "d",
      "photo": null
    },
    {
      "id": "cafd632d-d9ba-4617-8823-abc145a95e9c",
      "name": "e",
      "photo": null
    }
  ],
  "message": "Data are ready."
}

Activity

private val client = OkHttpClient()

override fun onCreate(savedInstanceState: Bundle?) {
   //....
   run("api_link")
}

fun run(url: String) {
  val request = Request.Builder()
    .url(url)
    .build()

  client.newCall(request).enqueue(object : Callback {
    override fun onFailure(call: Call, e: IOException) {}
    override fun onResponse(call: Call, response: Response) {
      Log.d("results: ", response.body()!!.string()) // does return the data

      // returning data in view?

    }
  })
}

Question

  1. How do I define what element to show the returned data?
  2. How do I loop those data? (what if returned data was object? like get post by id)

Update

I've made some changes to my code and now I'm getting org.json.JSONException: No value for services

code

fun run(url: String) {
        val request = Request.Builder()
            .url(url)
            .build()

        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {}
            override fun onResponse(call: Call, response: Response) {


                // added this part
                val list: ArrayList<Service> = ArrayList()
                getServices(response.body()!!.string(), list)
                //


                Log.d("results: ", response.body()!!.string())

                // Getting view element
                val textView: TextView = findViewById(R.id.recycler)
                textView.text = list.toString()
            }
        })
    }

fun getServices(response: String, list: ArrayList<Service>) { //service class
    var jsonObject = JSONObject(response)
    val jsonArray = jsonObject.getJSONArray("services")

    for (i in 0 until jsonArray.length()) {
        val jsonObject1 = jsonArray.getJSONObject(i)
        var listingObject = Service( //service class
            jsonObject1.getInt("id"),
            jsonObject1.getString("name"),
            jsonObject1.getString("image")
        )
        list.add(listingObject)
    }
}

Then I've add sercive class like:

class Service ( val id: Int, val name: String?, val image: String?) {
}

any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288

1 Answers1

1

Well, there are some steps between receive data from API and show it on your UI.

How do you want to show it? Populate a recyclerview? Show some info in toast? Show in some editText?

You'll need to parse JSON to some object, let's say Object "Data" with fields "id", "name" and "photo".

After your parse it, since you are already in your activity, you can show it in your view.

You'll need to get reference to your View and show it.

If you have an Edit Text, you can show it by calling:

  (your editText Reference).setText(data.name)
Filipe Oliveira
  • 850
  • 7
  • 13
  • I've updated my question (included your suggestion) would you mind check it? – mafortis Jan 20 '21 at 17:42
  • @mafortis I would suggest your "getServices" method return the arrayList, so you can write list = getServices(...). As you have more than one service in your list, you can't show everything in one editText. You can test your code by : textView.text = list[0].toString(). About JSON error, you can find more here: https://stackoverflow.com/questions/15477304/android-jsonexception-no-value-for – Filipe Oliveira Jan 20 '21 at 17:53
  • no don't make mistake these names are remained from many changes I've made :D I actually trying to use recycleview `findViewById(R.id.recycler)` my intention is to show a list of all items. – mafortis Jan 20 '21 at 17:56