json - 使用 kotlin 在 android studio 中进行 api 调用时遇到问题
问题描述
我想在使用 kotlin 的 android studio 中使用 json 对象请求进行 api 调用。这是api。
如何访问数组文章中的“名称”?我可以访问除“id”和“name”之外的所有其他内容。这是我的代码
val url = "https://news-api-don.herokuapp.com/api/v1?apiKey=20d14506791144cc8b424549c42068c0"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
{
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)
swipeRefreshLayout.isRefreshing = false
progressBar.visibility = View.GONE
},
{
Toast.makeText(this,"Something went wrong", Toast.LENGTH_LONG).show()
swipeRefreshLayout.isRefreshing = false
progressBar.visibility = View.GONE
}
)
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
}
解决方案
推荐阅读
- node.js - Nodejs - 如何在不使用像 multer 这样的中间件的情况下将从前端发送的文件对象保存到公用文件夹?
- macos - 应用迁移时出错,请尝试从命令行应用它们
- c# - 从 .net 4.7.2 winform 应用程序调用 .netcore2.1 库
- angular - 不同的路由,相同的组件 - 为什么组件不会在路由更改时重新加载?
- wordpress - Wordpress,使用 3rd 方 REST Api
- linux - 在while循环bash中进行grep
- reactjs - “this.props”在我的地图组件中使用 react 和 mapbox-gl 的 mouseup 事件函数中不起作用
- php - 在 Amazon AWS Cloud 9 服务器上安装 CodeIgniter 4
- macos - 如何将我的 applescript 部署到多个设备?
- angular - 带有 toHaveBeenCalledTimes 的 Jest 拦截器角度