How to fix an error GET request API in kotlin?



I have a problem when I start a request in Rick and Morty API.
I tried to launch the application in debug but I have an error 404
I don’t undestand why I receive an 404 error because the base URL is correct.
Thanks for you help !!

class MainActivity : AppCompatActivity() {

private val url = ""

override fun onCreate(savedInstanceState: Bundle?) {

        val retrofit = Retrofit.Builder()

        val service = retrofit.create(
        val characterRequest = service.listCharacters()

        characterRequest.enqueue(object : Callback<List<Character>> {
            override fun onResponse(call: Call<List<Character>>, response: Response<List<Character>>
            ) {
                val allCharacters = response.body()
                if (allCharacters != null) {
                    Log.i("Success","HERE is ALL CHARACTERS FROM RICK AND MORTY:")
                    for (c in allCharacters)
                        Log.i("Success","  one character : ${} : ${c.gender} ")
                else (
                        Log.i("Error","Empty ")



            override fun onFailure(call: Call<List<Character>>, t: Throwable) {
                Log.e("Error", "KO")


ApiService :

interface ApiService {

    fun listCharacters(): Call<List<Character>>

Character class:

class Character(val name : String , val gender : String) {

My dependencies :

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation ''
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
    implementation "com.squareup.okhttp3:okhttp:4.9.3"



I guess the error is in the way you defined response type of the listCharacters method. The response type should not be List<Character> as you defined. Try the next approach:

interface ApiService {

    fun listCharacters(): Call<ApiResponse>

data class ApiResponse(val results: List<Character>)

characterRequest.enqueue(object : Callback<ApiResponse> {
    override fun onResponse(call: Call<ApiResponse>, response: Response<ApiResponse>
    ) {
        val apiResponse: ApiResponse? = response.body()
        // use apiResponse

    override fun onFailure(call: Call<ApiResponse>, t: Throwable) {


Answered By – Sergey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More