Type mismatch. Required: List<DogResponse> Found: List<String> in kotlin retrofit

  android, gson, kotlin, retrofit2, type-mismatch

I am developing a new android where I am getting images by using retrofit in android but I am getting the following errors below
TypeMismatch error

following my json structure
json structure

below my DogResponse data class

data class DogResponse(
    @SerializedName("message")
    val message: List<String>,
    @SerializedName("status")
    val status: String
)

below my network logic

interface DogInterface {
    @GET("breed/hound/images")
  fun getDogImage(): Call<DogResponse>
}

below retrofit logic

object ServiceBuilder {
    private val client = OkHttpClient.Builder().build()

    private val retrofit = Retrofit.Builder()
        .baseUrl("https://dog.ceo/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build()

    fun<T> buildService(service: Class<T>): T{
        return retrofit.create(service)
    }
}

below my Adapter class

class DogAdapter(private val data: List<DogResponse>) : RecyclerView.Adapter<DogAdapter.MyViewHolder>()  {

    class MyViewHolder(val view: View): RecyclerView.ViewHolder(view){

        fun bind(dogResponse: DogResponse){

            val imageView = view.findViewById<ImageView>(R.id.imageView)

            Glide.with(view.context).load(dogResponse.message).centerCrop().into(imageView)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return MyViewHolder(v)
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(data[position])
    }


}

below my retrofit call in MainActivity.kt

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val request = ServiceBuilder.buildService(DogInterface::class.java)
    val call = request.getDogImage()
    call.enqueue(object : Callback<DogResponse> {
        override fun onResponse(call: Call<DogResponse>, response: Response<DogResponse>) {
            if (response.isSuccessful) {
                progress_bar.visibility = View.GONE
                recycler_view.apply {
                    setHasFixedSize(true)
                    layoutManager = LinearLayoutManager([email protected])
                    adapter = DogAdapter(response.body().message)
                }
            }
        }

        override fun onFailure(call: Call<DogResponse>, t: Throwable) {
            Toast.makeText([email protected], "${t.message}", Toast.LENGTH_SHORT).show()
        }
    })
}

}

I want to know where I am making a mistake

Source: Android Questions

One Reply to “Type mismatch. Required: List<DogResponse> Found: List<String> in kotlin retrofit”

LEAVE A COMMENT