Search code examples
androidapirestkotlinretrofit

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 = "https://rickandmortyapi.com/api/"

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


        val retrofit = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(MoshiConverterFactory.create())
            .build()

        val service = retrofit.create(ApiService::class.java)
        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.name} : ${c.gender} ")
                }
                else (
                        Log.i("Error","Empty ")

                        )

            }

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

        })

ApiService :

interface ApiService {

    @GET("character")
    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 'com.google.android.material:material:1.4.0'
    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"

}

Solution

  • 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 {
    
        @GET("character")
        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) {
        }
    
    })