"BasicNetwork.performRequest: Unexpected response code 403 for in android studio " can any one help me with this error

Viewed 271

my project link -> https://github.com/up1512001/news-app-demo [this is error showing by android studio][1]

in error section it doesn't show any error but when i am running it on my phone it gives 403 error and i have checked network permission but when i used Log.d() in jsonobjectrequest then it doesn't giving any log that means i guess error should be in framing json response. [1]: https://i.stack.imgur.com/ZZoMc.png

1 Answers

try this solution Source

private fun fetchData() {

        val url =
            "https://newsapi.org/v2/top-headlines?country=in&category=science&apiKey=1f4a12d2698e432ea9cf18126dcc7acd"
        val jsonObjectRequest = object : JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            {
                Log.e("TAG", "fetchData: $it")
                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)
            },

            {
                Log.d("Error occur", "Try again..." + it.networkResponse.statusCode)
            }
        ) {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers["User-Agent"] = "Mozilla/5.0"
                return headers
            }
        }
        MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
    }
Related