我正在尝试从下面的 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)
}
我遇到了同样的错误,我通过将标头 “Mozilla/5.0” 传递给请求来解决它。
这可以在您的代码上以这种方式完成。
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() 方法。
这个答案帮助我使用 kotlin 在 Volley 请求中添加自定义标头。
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0");
return headers;
}
Java版本供有需要的人使用。
我无知,但你是否知道这是什么?
@覆盖
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0");
return headers;
}