0

I want to make api calls using json object request in android studio using kotlin. This is the api.Api image

How to access the 'name' in the array articles ?. I can access all the other things except 'id' and 'name'. This is my code

val url = "https://news-api-don.herokuapp.com/api/v1?apiKey=20d14506791144cc8b424549c42068c0"

    val jsonObjectRequest = JsonObjectRequest(
        Request.Method.GET, url, null,
        {
            val newsJsonArray = it.getJSONArray("articles")
            val newsArray = ArrayList<News>()
            for(i in 0 until newsJsonArray.length()) {
                val newsJsonObject = newsJsonArray.getJSONObject(i)
                val news = News(
                    newsJsonObject.getString("title"),
                    newsJsonObject.getString("author"),
                    newsJsonObject.getString("url"),
                    newsJsonObject.getString("urlToImage")
                )
                newsArray.add(news)

            }
            mAdapter.updateNews(newsArray)
            swipeRefreshLayout.isRefreshing = false
            progressBar.visibility = View.GONE

        },
        {
            Toast.makeText(this,"Something went wrong", Toast.LENGTH_LONG).show()
            swipeRefreshLayout.isRefreshing = false
            progressBar.visibility = View.GONE
        }
    )


    MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)

}

0 Answers0