Retrofit2 call.enqueue(对象:Callback<JSONModel>{...}) 在 IDE 中显示为错误



我是编程新手。我正在开发从API获取JSON数据的应用程序。为此,我使用改装和Gson。我遇到的问题是,尽管之前的项目中使用了相同的代码,但安卓工作室还是向我显示了错误。我仔细检查了导入并使用了正确的包,但在IDE上仍然出现了错误。问题出现在文件AppRepository.kt中我的改装API接口BookAPI.kt:

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

这是我的改装实例,用AppModule.kt:注入dagger柄

...
@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)
}
...
}

这里,在AppRepository.kt上,我在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
}
})
...
}
}

我的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"
}

如何解决此问题?

事实证明,我在API接口上使用了错误的返回类型

BookAPI.kt应该是这样的:

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

最新更新