Java 在活动中使用截击进行两次API调用…良好实践?
我正在抓取一些口袋妖怪的数据,也想抓取它们的图像,并在同一活动中滑动。图像无法直接获取,我必须从口袋妖怪url对象获取图像的url 我的意思是我必须先用Java 在活动中使用截击进行两次API调用…良好实践?,java,android,android-volley,android-adapter,android-glide,Java,Android,Android Volley,Android Adapter,Android Glide,我正在抓取一些口袋妖怪的数据,也想抓取它们的图像,并在同一活动中滑动。图像无法直接获取,我必须从口袋妖怪url对象获取图像的url 我的意思是我必须先用https://pokeapi.co/api/v2/limit?=10从响应中,我按id获取每个口袋妖怪图像urlhttps://pokeapi.co/api/v2/pokemon/1 我能够在我的适配器中成功地做到这一点,但我想知道这是否是最佳实践,因为我担心如果数量较大,它会滞后 适配器在下面 类ApiAdapter(内部变量活动:MainA
https://pokeapi.co/api/v2/limit?=10
从响应中,我按id获取每个口袋妖怪图像urlhttps://pokeapi.co/api/v2/pokemon/1
我能够在我的适配器中成功地做到这一点,但我想知道这是否是最佳实践,因为我担心如果数量较大,它会滞后
适配器在下面
类ApiAdapter(内部变量活动:MainActivity):RecyclerView.Adapter(){
private val queue=Volley.newRequestQueue(活动)
初始化{
loadData()
}
val dataInstance=ArrayList()
私有数据{
val url=”https://pokeapi.co/api/v2/pokemon?limit=5"
val jsonObjectRequest=jsonObjectRequest(
Request.Method.GET,url,null,
Response.Listener{Response->
试一试{
val jsonArray=response.getJSONArray(“结果”)
lateinit变量名称:String
lateinit var pokeUrl:字符串
lateinit var jsonCities:JSONArray
for(在0中输入i直到jsonArray.length()){
val jsonObject=jsonArray.getJSONObject(i)
name=jsonObject.getString(“名称”)
pokurl=jsonObject.getString(“url”)
添加(DataClass(名称,pokeUrl))
}
notifyDataSetChanged()
}
捕获(错误:JSONException){
错误。printStackTrace()
}
},
Response.ErrorListener{error->
//TODO:句柄错误
Log.e(“Api”,“错误:$error”)
}
)
//将请求添加到RequestQueue。
添加(jsonObjectRequest)
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):ApiViewHolder{
val layout=LayoutInflater.from(parent.context).充气(R.layout.data_行,parent,false)
返回ApiViewHolder(布局)
}
覆盖BindViewHolder(holder:ApiViewHolder,位置:Int){
val current=dataInstance.get(位置)
holder.name.text=current.name
fun loadImageData(){
val url=current.url
val jsonObjectRequest=jsonObjectRequest(
Request.Method.GET,url,null,
Response.Listener{Response->
试一试{
val jsonResponse=response.getJSONObject(“精灵”)
val picUrl=jsonResponse.optString(“前端默认值”)
使用(活动)滑动。加载(picUrl)。放入(holder.image)
//notifyDataSetChanged()
Toast.makeText(活动“res:$picUrl”,Toast.LENGTH\u LONG.show())
}
捕获(错误:JSONException){
错误。printStackTrace()
}
},
Response.ErrorListener{error->
Log.e(“Api”,“错误:$error”)
}
)
//将请求添加到RequestQueue。
添加(jsonObjectRequest)
}
loadImageData()
}
重写getItemCount():Int{
返回dataInstance.size
}
类ApiViewHolder(dataView:View):RecyclerView.ViewHolder(dataView){
内部变量名称=dataView.findViewById(R.id.name)
内部var image=dataView.findviewbyd(R.id.pokemon\u image)
}
}
也许有更好的办法来解决这个问题。非常感谢您的建议一些一般性的注意事项和建议:
- 您正在从
触发API调用。这将在每次循环后绑定视图时触发它。您需要将其从适配器中拉出,或者实现一些缓存。一般来说,不要在没有缓存的情况下将API放在适配器中,这会导致灾难onBindViewHolder
- 您正在从适配器中调用
。这通常也被认为是不好的做法。工作流程应该是适配器只接收视图状态数据,然后通过外部notifyDatasetChanged()
将该视图状态数据应用于其子级notifyDatasetChanged
https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png
如果您只需将该URL中的编号4
替换为口袋妖怪的ID,就可以避免进行额外的API调用,只需将手动URL直接传递给适配器。然后在完整细节活动或片段中抓取完整的口袋妖怪对象。
只是个主意
class ApiAdapter (internal var activity: MainActivity):RecyclerView.Adapter<ApiAdapter.ApiViewHolder>(){
private val queue = Volley.newRequestQueue(activity)
init{
loadData()
}
val dataInstance = ArrayList<DataClass>()
private fun loadData(){
val url = "https://pokeapi.co/api/v2/pokemon?limit=5"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
Response.Listener { response ->
try {
val jsonArray = response.getJSONArray("results")
lateinit var name:String
lateinit var pokeUrl: String
lateinit var jsonCities: JSONArray
for(i in 0 until jsonArray.length()){
val jsonObject = jsonArray.getJSONObject(i)
name = jsonObject.getString("name")
pokeUrl = jsonObject.getString("url")
dataInstance.add(DataClass(name, pokeUrl))
}
notifyDataSetChanged()
}
catch (error: JSONException){
error.printStackTrace()
}
},
Response.ErrorListener { error ->
// TODO: Handle error
Log.e("Api", "error: $error")
}
)
// Add the request to the RequestQueue.
queue.add(jsonObjectRequest)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ApiViewHolder{
val layout = LayoutInflater.from(parent.context).inflate(R.layout.data_row, parent, false)
return ApiViewHolder(layout)
}
override fun onBindViewHolder(holder:ApiViewHolder, position:Int){
val current = dataInstance.get(position)
holder.name.text = current.name
fun loadImageData(){
val url = current.url
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET, url, null,
Response.Listener { response ->
try {
val jsonResponse = response.getJSONObject("sprites")
val picUrl = jsonResponse.optString("front_default")
Glide.with(activity).load(picUrl).into(holder.image)
// notifyDataSetChanged()
Toast.makeText(activity, "res: $picUrl", Toast.LENGTH_LONG).show()
}
catch (error: JSONException){
error.printStackTrace()
}
},
Response.ErrorListener { error ->
Log.e("Api", "error: $error")
}
)
// Add the request to the RequestQueue.
queue.add(jsonObjectRequest)
}
loadImageData()
}
override fun getItemCount(): Int {
return dataInstance.size
}
class ApiViewHolder(dataView: View): RecyclerView.ViewHolder(dataView){
internal var name = dataView.findViewById<TextView>(R.id.name)
internal var image = dataView.findViewById<ImageView>(R.id.pokemon_image)
}
}