Search code examples
androidkotlinandroid-volley

Value Forbidden of type java.lang.String cannot be converted to JSONObject


When I try to get historical data from yahoo like this:

private fun getStockHistoricalData(){
    val queue = VolleySingleton.getInstance(this)
    val query = "https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1635962336&period2=1667498336&interval=1d&events=history&includeAdjustedClose=true"
    val request = StringRequest( Request.Method.GET,query,
        { response ->
            println("response = $response")
        },
        { error ->
            println("Error ${parseVolleyError(error)}")
        }
    )
    request.setShouldCache(false)
    queue.addToRequestQueue(request)
}

I'm getting following error: Value Forbidden of type java.lang.String cannot be converted to JSONObject and this:

NetworkUtility.shouldRetryException: Unexpected response code 403 for https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1635962336&period2=1667498336&interval=1d&events=history&includeAdjustedClose=true

The query is working well. You can try on postman or browser. I also tried different variations like JsonObjectRequest or Method.POST instead of GET. Retrofit also didn't work. It was even working before.


Solution

  • Try overriding your header like:

    override fun getHeaders(): MutableMap<String, String> {
        val header = HashMap<String, String>()
        header["User-Agent"] = "Mozilla/5.0"
        return header
    }