API 在 activity 中使用 volley 调用两次...好的做法?

Making API call twice with volley in an activity...Good Practice?

我正在获取有关一些宠物小精灵的数据,并且还想抓取它们的图像并在同一 activity 中滑行。无法直接获取图像,我必须从 pokemon url 对象中获取 url 图像。

我的意思是我必须首先从响应中获取带有 https://pokeapi.co/api/v2/limit?=10 的宠物小精灵 我通过 id https://pokeapi.co/api/v2/pokemon/1

获得每个宠物小精灵图像 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)

    }
}

也许有更好的方法来解决这个问题。非常感谢您的建议

一些一般注意事项和建议:

  • 您正在触发来自 onBindViewHolder 的 API 呼叫。这将在每次回收后绑定视图时触发它。您将需要将其从适配器中拉出,或者实施一些缓存。通常,永远不要在没有缓存的情况下将 API 放入适配器中,否则会导致灾难。
  • 您正在从适配器中调用 notifyDatasetChanged()。这通常也被认为是不好的做法。工作流应该是适配器只接收视图状态数据,然后通过外部 notifyDatasetChanged.
  • 将该视图状态数据应用于它的子级

不过,对于您的具体情况,您实际上根本不需要额外的 API 调用来获取口袋妖怪的图像。 PokeAPI 中的所有神奇宝贝都使用相同的图像 URL 格式,例如:
https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png

如果您只是将 URL 中的数字 4 替换为口袋妖怪的 ID,则可以避免进行额外的 API 调用,只需传递该手册 URL 直接到适配器。然后在完整的细节 activity 或片段中获取完整的 pokemon 对象。 只是一个想法。