问题描述
我正在尝试从以下 URL 获取 json 数据
Im trying to get the json data from below URL
但是当我运行应用程序时它会显示
But when I run the app it shows
31029-31125 E/Volley: [228776] NetworkUtility.shouldRetryException: Unexpected response code 403 for https://newsapi.org/v2/everything?q=tesla&from=2021-02-23&sortBy=publishedAt&apiK 20ey=c9e4ed47388d413c8af23fc46a330f8e
31029-31125 E/Volley: [228776] NetworkUtility.shouldRetryException: Unexpected response code 403 for https://newsapi.org/v2/everything?q=tesla&from=2021-02-23&sortBy=publishedAt&apiK 20ey=c9e4ed47388d413c8af23fc46a330f8e
但是当我在 chrome 浏览器中输入 URL 时,它会正常显示 json 数据,但我在我的应用程序中得到了同样的东西这是我在 kotlin 中的代码
But when I enter the URL in chrome browser it shows the json data normally but I got getting the same thing in my apphere is my code in kotlin
fun getNews(context: Context){
var queue: RequestQueue = Volley.newRequestQueue(context)
val url = "https://newsapi.org/v2/everything?q=tesla&from=2021-05-07&sortBy=publishedAt&apiKey=c9e4ed47388d413c8af23fc46a330f8e"
val request = JsonObjectRequest(
Request.Method.GET, url, null,
{ response ->
var list: MutableList<newModel> = mutableListOf<newModel>()
try {
var rootArray: JSONArray = response.getJSONArray("articles")
for(i in 0 until response.length()){
var dataObject: JSONObject = rootArray.get(i) as JSONObject
list.add(newModel(dataObject.getString("urlToImage") , dataObject.getString("title") , dataObject.getString("description") , dataObject.getString("url")))
}
} catch (e: Exception) {
Toast.makeText(
context,
"error while parsing the jsonObject/array",
Toast.LENGTH_LONG
).show()
}
callBack.gotTheNewsData(list)
}) { error ->
Toast.makeText(context, "Error in responce", Toast.LENGTH_SHORT).show()
}
queue.add(request)
}
推荐答案
我遇到了同样的错误,我通过向请求传递标头 Mozilla/5.0" 解决了这个问题.
I was facing the same error and I solved it by passing a header "Mozilla/5.0" to the request.
这可以在您的代码上以这种方式完成.
This can be done in this way on your code.
fun getNews(context: Context){
var queue: RequestQueue = Volley.newRequestQueue(context)
val url = "Your URL here"
val request = object: JsonObjectRequest(
Request.Method.GET, url, null,
{ response ->
var list: MutableList<newModel> = mutableListOf<newModel>()
try {
var rootArray: JSONArray = response.getJSONArray("articles")
for(i in 0 until response.length()){
var dataObject: JSONObject = rootArray.get(i) as JSONObject
list.add(newModel(dataObject.getString("urlToImage") , dataObject.getString("title") , dataObject.getString("description") , dataObject.getString("url")))
}
} catch (e: Exception) {
Toast.makeText(
context,
"error while parsing the jsonObject/array",
Toast.LENGTH_LONG
).show()
}
callBack.gotTheNewsData(list)
}) { error ->
Toast.makeText(context, "Error in responce", Toast.LENGTH_SHORT).show()
}
// overriding function to add Headers.
{
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["User-Agent"]="Mozilla/5.0"
return headers
}
}
queue.add(request)
}
在构造请求之前使用 object 关键字很重要,以便能够覆盖 getHeaders() 方法.
It is important to use the object keyword before the construction of the request in order to be able to override the getHeaders() method.
这个答案帮助了我使用 kotlin 在 Volley 请求中添加自定义标头.
This answer helped me in adding a custom header in Volley request with kotlin.
这篇关于意外的响应代码 403(但在浏览器中工作正常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!