如何使用 retrofit2 在数据 class 中显示类别 - Kotlin

how to display categories in data class with retrofit2 - Kotlin

我正在尝试使用 koltin

中的 retrofit2 显示来自 API 的数据

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

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

我尝试在我的数据 class 中仅调用(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 个数组 class?

我的 activity 中的适配器和代码是正确的,因为我用另一个 API

测试过它

所以我做错了什么?

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

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

而不是这个:

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

这样做:

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

检查点

  • 你要的数据在List of List里
    • json数组 > json数组 > IimfinderDataJson IimfinderData 列表
  • eventPhoto 是对象,不是字符串

试试这个代码

Call<List<IimfinderData>> -> Call<List<IlmFinders>>

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
)

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

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

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

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

而不是使用此 class 作为数据 class 中的数据列表:

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

访问此数据class,功能如下:

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

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

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