【问题标题】:how to display categories in data class with retrofit2 - Kotlin如何使用 retrofit2 在数据类中显示类别 - Kotlin
【发布时间】:2021-08-14 13:00:21
【问题描述】:

我正在尝试使用 koltin 中的 retrofit2 显示来自 API 的数据

但 API 显示在我调用的数据之前有 categoryId,如下所示:

  [
   {
    "categoryId": 0,
    "categoryName": "string",
    "ilmFinders": [
    {
    "eventId": 0,
    "eventName": "string",
    "eventLocation": "string",
    "eventPhoto": {},
    "eventDescription": "string",
    "eventDate": "string",
    "eventLink": "string",
    "isFavourite": {}
    }
    ]
   }
  ]

我尝试在我的数据类中仅调用 (eventName, eventPhoto, eventLink),如下所示:

data class IimfinderData(

val eventLink: String,
val eventPhoto: String,
val eventName: String

)

API接口:

        interface finderService {
        @GET(" ")
        fun getServices() : Call<List<IimfinderData>>
        companion object Factory {
        fun create(): finderService {
        val retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("PRIVATE URL")
            .build()

        return retrofit.create(finderService::class.java)
        }
        }
        }

适配器:

class IimfinderAdapter(var countryList: List<IimfinderData>, var activity: MainActivity): RecyclerView.Adapter<IimfinderAdapter.ViewHolder>(){
lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IimfinderAdapter.ViewHolder {
    context = parent.context!!
    val view = LayoutInflater.from(context).inflate(R.layout.list_item2, parent, false)
    return ViewHolder(view)
}

override fun getItemCount(): Int {
    return countryList.size
}

override fun onBindViewHolder(holder: IimfinderAdapter.ViewHolder, position: Int) {
    holder.eventName.text = countryList[position].eventName
    holder.eventLink.text = countryList[position].eventLink




    Picasso.with(activity)
        .load(countryList[position].eventPhoto)
        .error(R.drawable.qiblacompass)
        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
        .networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
        .into(holder.eventPhoto)




}

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    val eventName: TextView = itemView.findViewById(R.id.title_tv2)
    val eventLink: TextView = itemView.findViewById(R.id.tv_url2)
    val eventPhoto: ImageView = itemView.findViewById(R.id.thumbnail_tv2)

}

}

我运行应用程序后,它显示数据为空

我需要调用“categoryId”吗?

如果是,我如何在同一个数据类中调用 2 个数组?

适配器和我活动中的代码是正确的,因为我使用另一个 API 对其进行了测试

那么我在这里做错了什么?

------------------- 解决方案--------------- ---------

参考下面的答案,我必须更改从适配器调用数据的方式

而不是这个:

holder.eventName.text = countryList[position].eventName

这样做:

holder.eventName.text = countryList[position].ilmFinders[position].eventName

【问题讨论】:

  • 您没有正确解析响应。 “eventName”在“ilmFinders”数组之外。而且“eventPhoto”不是字符串而是JsonObject
  • @BhargavThanki 谢谢你,但你能更清楚一点吗?因为我还是 kotlin 的新手,所以我以相同的方式调用了不同的数据类并且它正在工作,但是这个它给了我“null”
  • 你能显示你的service接口代码吗?
  • @liveAnyway 我已经添加了接口

标签: java android api kotlin retrofit2


【解决方案1】:

当您尝试在 API 响应中访问 jsonArry 时,您可以执行以下操作:

为“IimfinderData”创建数据类,序列化为:

data class ResponseData(
@SerializedName("name")
val userName: String,
@SerializedName("age")
val userAge: Integer,
@SerializedName("date_of_birth")
val userDOB: String)

比使用这个类作为数据类中的数据列表:

data class Response(
@SerializedName("userData")
val ilmFinders: List<ResponseData>)

使用以下功能访问此数据类:

fun getServices() : Call<List<Response>>

如果您没有使用与响应数据相同的名称,请确保使用 @SerializedName 注释

希望这能解决你的问题!!

【讨论】:

    【解决方案2】:

    检查点

    • 您想要的数据在列表列表中
      • jsonArray > jsonArray > IimfinderDataJson IimfinderData 按列表
    • eventPhoto 是对象,不是字符串

    试试这个代码

    Call&lt;List&lt;IimfinderData&gt;&gt; -> Call&lt;List&lt;IlmFinders&gt;&gt;

    fun getServices() : Call<List<IlmFinders>>
    

    data class IlmFinders(
        @SerializedName("ilmFinders")
        val ilmFinders: List<IimfinderData>
    )
    
    data class IimfinderData(
        @SerializedName("eventLink")
        val eventLink: String,
        @SerializedName("eventPhoto")
        val eventPhoto: Any,
        @SerializedName("eventName")
        val eventName: String
    )
    

    如果您的数据类属性名称与json元素相同,则不需要@SerializedName

    【讨论】:

    • 谢谢,但是当我更改 Call> -> Call> 我无法从适配器调用数据
    • 是的,我试过了,当我更新你提到的代码时,我在“countryList [position].eventName”行中的适配器中出现错误,它找不到“eventName”,同样适用其他数据
    • 我的意思是,在您尝试过的帖子上更新您的代码。
    • 我只需要从适配器调用数据的正确位置,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多