Retrofit2 call.enqueue(object : Callback<JSONModel>{...}) is showing as error in IDE

Viewed 20

I am new to programming. I am working on app that gets JSON data from API. For that I am using Retrofit and Gson. The issue I am having is that Android Studio is showing me errors even though the same code worked on previous project. I double checked imports and I used correct packages but still I get the error on IDE. The issue occurs in file AppRepository.kt My Retrofit API interface BookAPI.kt:

interface BookAPI {
    @GET("books?$OTHER_PARAMS")
    fun getBookDetailsByISBN(@Query("bibkeys") isbn: String ) : Call<ResponseBody>
    companion object {
        const val OTHER_PARAMS = ...
    }
}

Here is my retrofit instance that gets injected with dagger hilt with AppModule.kt:

...

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Provides
    @Singleton
    fun provideBookAPI(): BookAPI {
        return Retrofit.Builder()
            .baseUrl(BookAPI.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(BookAPI::class.java)
    }
...
}

Here, on AppRepository.kt, I get that error on line call.enqueue(...

//imports related to retrofit
import com.fracta7.e_bookshelf.data.local.database.book.Book
import com.fracta7.e_bookshelf.data.remote.BookAPI
import com.fracta7.e_bookshelf.domain.model.book.JSONModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject

class AppRepository @Inject constructor(
    private val bookAPI: BookAPI 
   )

    fun getBookByISBN(isbn: String): Book {
        var remoteInfo: JSONModel? = null
        val call = bookAPI.getBookDetailsByISBN(isbn)
//here I get error on ide: Type mismatch: required Callback<ResponseBody!>!, found: ``
//                  -vvvvvvvvvvvvvvvvvvvvvvvvvvvv- 
        call.enqueue(object : Callback<JSONModel> {
            override fun onResponse(call: Call<JSONModel>, response: Response<JSONModel>) {
                //on response
            }

            override fun onFailure(call: Call<JSONModel>, t: Throwable) {
                //on failure
            }
        })
        ...
    }
}

My build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {...}

dependecies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation 'androidx.compose.material3:material3:1.0.0-beta01'
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.5.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    //Room
    def room_version = '2.4.3'
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    //dagger hilt
    implementation 'com.google.dagger:hilt-android:2.43.2'
    kapt 'com.google.dagger:hilt-android-compiler:2.43.2'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

    //viewmodel
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'

    //retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'

    //coroutines
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
}

How can I resolve this issue?

1 Answers

Turns out, I used wrong return type on API interface

BookAPI.kt should look like this:

interface BookAPI {
    @GET("books?$OTHER_PARAMS")
    fun getBookDetailsByISBN(@Query("bibkeys") isbn: String ) : Call<JSONModel>
    companion object {
        const val OTHER_PARAMS = ...
    }
}

Related