【发布时间】:2021-08-24 17:38:49
【问题描述】:
我正在尝试从以下 URL 获取 json 数据
但是当我运行应用程序时它会显示
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 中的代码
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)
}
【问题讨论】:
-
您刚刚将您的 api 密钥发布到互联网上,因此现在任何人都可以使用它。希望不会有什么影响
-
仅供测试
-
你能打印出错误回调中的错误吗?它应该包含更多关于出了什么问题的信息?
-
com.android.volley.AuthFailureError
-
即使是为了测试,您现在也需要生成一个新密钥并使您发布的密钥无效...
标签: android kotlin android-volley http-status-code-403