尝试使用 Retrofit 发出获取请求时无法为 class java.lang.Object 创建调用适配器
Unable to create call adapter for class java.lang.Object while trying to make get request using Retrofit
我在尝试向 NewsApi.org api 发出获取请求时遇到以下错误。根据他们的文档,一切似乎都是正确的,我的设置与我在 github.
上找到的许多教程和一些示例基本相同
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newsfeed, PID: 12360
java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.Object
for method NewsApi.getBreakingNews
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy1.getBreakingNews(Unknown Source)
at com.example.newsfeed.data.API.NewsApi$DefaultImpls.getBreakingNews$default(NewsApi.kt:11)
at com.example.newsfeed.util.NewsPagingSource.load(NewsPagingSource.kt:25)
at androidx.paging.PageFetcherSnapshot.doInitialLoad(PageFetcherSnapshot.kt:275)
at androidx.paging.PageFetcherSnapshot$pageEventFlow.invokeSuspend(PageFetcherSnapshot.kt:160)
at androidx.paging.PageFetcherSnapshot$pageEventFlow.invoke(PageFetcherSnapshot.kt)
at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow.invokeSuspend(CancelableChannelFlow.kt:30)
at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow.invoke(CancelableChannelFlow.kt)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invokeSuspend(SimpleChannelFlow.kt:57)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invoke(SimpleChannelFlow.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:194)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invokeSuspend(SimpleChannelFlow.kt:52)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:357)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at androidx.lifecycle.BlockRunner.maybeRun(CoroutineLiveData.kt:174)
at androidx.lifecycle.CoroutineLiveData.onActive(CoroutineLiveData.kt:240)
at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:390)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:466)
at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:88)
at androidx.fragment.app.Fragment.performStart(Fragment.java:3028)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToS
问题似乎出在我的 api 界面上,但我会分享更多代码以防有用。目前我的api界面如下:
package com.example.newsfeed.data.API
import com.example.newsfeed.data.API.ApiKey.Companion.API_KEY
import com.example.newsfeed.data.models.NewsApiResponse
import retrofit2.http.GET
import retrofit2.http.Query
interface NewsApi {
@GET("/v2/top-headlines")
suspend fun getBreakingNews(
@Query("country")
country: String = "us",
@Query("page")
pageNum: Int = 1,
@Query("apiKey")
apiKey: String = API_KEY,
@Query("pageSize")
pageSize: Int
): NewsApiResponse
}
用于 hilt 注入的网络模块:
package com.example.newsfeed.di
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.util.Constants.Companion.BASE_URL
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun provideNewsApi(retrofit: Retrofit): NewsApi =
retrofit.create(NewsApi::class.java)
}
我的 PagingSource 设置:
package com.example.newsfeed.util
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bumptech.glide.load.HttpException
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.data.models.Article
import com.example.newsfeed.util.Constants.Companion.NEWSAPI_STARTING_PAGE_INDEX
import java.io.IOException
class NewsPagingSource(
private val newsapi: NewsApi
): PagingSource<Int, Article>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
val position = params.key ?: NEWSAPI_STARTING_PAGE_INDEX
return try {
val response = newsapi.getBreakingNews(pageNum = position, pageSize = params.loadSize)
val articles = response.articles
LoadResult.Page(
data = articles,
prevKey = if (position == NEWSAPI_STARTING_PAGE_INDEX) null else position - 1,
nextKey = if (articles.isEmpty()) null else position + 1
)
} catch (exception: IOException) {
LoadResult.Error(exception)
} catch (exception: HttpException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, Article>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
}
}
}
我的存储库:
package com.example.newsfeed.repository
import androidx.lifecycle.asLiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.util.NewsPagingSource
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NewsRepository @Inject constructor(
private val newsApi: NewsApi
) {
fun getBreakingNews() =
Pager(
config = PagingConfig(
pageSize = 20,
maxSize = 100,
enablePlaceholders = false
),
pagingSourceFactory = { NewsPagingSource(newsApi) }
).flow.asLiveData()
}
视图模型:
package com.example.newsfeed.viewmodels
import androidx.lifecycle.ViewModel
import com.example.newsfeed.repository.NewsRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject
@HiltViewModel
class NewsScreenViewModel @Inject constructor(
val newsRepository: NewsRepository,
private val ApplicationScope: CoroutineScope
): ViewModel() {
val articles = newsRepository.getBreakingNews()
}
片段:
package com.example.newsfeed.ui
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.newsfeed.R
import com.example.newsfeed.adapters.NewsArticlesAdapter
import com.example.newsfeed.databinding.FragmentNewsScreenBinding
import com.example.newsfeed.viewmodels.NewsScreenViewModel
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class NewsScreenFragment : Fragment(R.layout.fragment_news_screen) {
private val viewModel: NewsScreenViewModel by viewModels()
private var _binding: FragmentNewsScreenBinding? = null
private val binding get() = _binding!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentNewsScreenBinding.bind(view)
val adapter = NewsArticlesAdapter()
binding.apply {
newsRecyclerView.setHasFixedSize(true)
newsRecyclerView.adapter = adapter
}
viewModel.articles.observe(viewLifecycleOwner) {
adapter.submitData(viewLifecycleOwner.lifecycle, it)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
您是 运行 没有协程支持的旧版 Retrofit。
要启用对 suspend
函数的支持,请将 Retrofit 升级到版本 2.6.0
或更高版本。
我在尝试向 NewsApi.org api 发出获取请求时遇到以下错误。根据他们的文档,一切似乎都是正确的,我的设置与我在 github.
上找到的许多教程和一些示例基本相同E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newsfeed, PID: 12360
java.lang.IllegalArgumentException: Unable to create call adapter for class java.lang.Object
for method NewsApi.getBreakingNews
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:234)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy1.getBreakingNews(Unknown Source)
at com.example.newsfeed.data.API.NewsApi$DefaultImpls.getBreakingNews$default(NewsApi.kt:11)
at com.example.newsfeed.util.NewsPagingSource.load(NewsPagingSource.kt:25)
at androidx.paging.PageFetcherSnapshot.doInitialLoad(PageFetcherSnapshot.kt:275)
at androidx.paging.PageFetcherSnapshot$pageEventFlow.invokeSuspend(PageFetcherSnapshot.kt:160)
at androidx.paging.PageFetcherSnapshot$pageEventFlow.invoke(PageFetcherSnapshot.kt)
at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow.invokeSuspend(CancelableChannelFlow.kt:30)
at androidx.paging.CancelableChannelFlowKt$cancelableChannelFlow.invoke(CancelableChannelFlow.kt)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invokeSuspend(SimpleChannelFlow.kt:57)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invoke(SimpleChannelFlow.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:194)
at androidx.paging.SimpleChannelFlowKt$simpleChannelFlow$producer.invokeSuspend(SimpleChannelFlow.kt:52)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:357)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:27)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
at androidx.lifecycle.BlockRunner.maybeRun(CoroutineLiveData.kt:174)
at androidx.lifecycle.CoroutineLiveData.onActive(CoroutineLiveData.kt:240)
at androidx.lifecycle.LiveData.changeActiveCounter(LiveData.java:390)
at androidx.lifecycle.LiveData$ObserverWrapper.activeStateChanged(LiveData.java:466)
at androidx.lifecycle.LiveData$LifecycleBoundObserver.onStateChanged(LiveData.java:425)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
at androidx.fragment.app.FragmentViewLifecycleOwner.handleLifecycleEvent(FragmentViewLifecycleOwner.java:88)
at androidx.fragment.app.Fragment.performStart(Fragment.java:3028)
at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToS
问题似乎出在我的 api 界面上,但我会分享更多代码以防有用。目前我的api界面如下:
package com.example.newsfeed.data.API
import com.example.newsfeed.data.API.ApiKey.Companion.API_KEY
import com.example.newsfeed.data.models.NewsApiResponse
import retrofit2.http.GET
import retrofit2.http.Query
interface NewsApi {
@GET("/v2/top-headlines")
suspend fun getBreakingNews(
@Query("country")
country: String = "us",
@Query("page")
pageNum: Int = 1,
@Query("apiKey")
apiKey: String = API_KEY,
@Query("pageSize")
pageSize: Int
): NewsApiResponse
}
用于 hilt 注入的网络模块:
package com.example.newsfeed.di
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.util.Constants.Companion.BASE_URL
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
@Provides
@Singleton
fun provideNewsApi(retrofit: Retrofit): NewsApi =
retrofit.create(NewsApi::class.java)
}
我的 PagingSource 设置:
package com.example.newsfeed.util
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bumptech.glide.load.HttpException
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.data.models.Article
import com.example.newsfeed.util.Constants.Companion.NEWSAPI_STARTING_PAGE_INDEX
import java.io.IOException
class NewsPagingSource(
private val newsapi: NewsApi
): PagingSource<Int, Article>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Article> {
val position = params.key ?: NEWSAPI_STARTING_PAGE_INDEX
return try {
val response = newsapi.getBreakingNews(pageNum = position, pageSize = params.loadSize)
val articles = response.articles
LoadResult.Page(
data = articles,
prevKey = if (position == NEWSAPI_STARTING_PAGE_INDEX) null else position - 1,
nextKey = if (articles.isEmpty()) null else position + 1
)
} catch (exception: IOException) {
LoadResult.Error(exception)
} catch (exception: HttpException) {
LoadResult.Error(exception)
}
}
override fun getRefreshKey(state: PagingState<Int, Article>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
}
}
}
我的存储库:
package com.example.newsfeed.repository
import androidx.lifecycle.asLiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.example.newsfeed.data.API.NewsApi
import com.example.newsfeed.util.NewsPagingSource
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class NewsRepository @Inject constructor(
private val newsApi: NewsApi
) {
fun getBreakingNews() =
Pager(
config = PagingConfig(
pageSize = 20,
maxSize = 100,
enablePlaceholders = false
),
pagingSourceFactory = { NewsPagingSource(newsApi) }
).flow.asLiveData()
}
视图模型:
package com.example.newsfeed.viewmodels
import androidx.lifecycle.ViewModel
import com.example.newsfeed.repository.NewsRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject
@HiltViewModel
class NewsScreenViewModel @Inject constructor(
val newsRepository: NewsRepository,
private val ApplicationScope: CoroutineScope
): ViewModel() {
val articles = newsRepository.getBreakingNews()
}
片段:
package com.example.newsfeed.ui
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.newsfeed.R
import com.example.newsfeed.adapters.NewsArticlesAdapter
import com.example.newsfeed.databinding.FragmentNewsScreenBinding
import com.example.newsfeed.viewmodels.NewsScreenViewModel
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class NewsScreenFragment : Fragment(R.layout.fragment_news_screen) {
private val viewModel: NewsScreenViewModel by viewModels()
private var _binding: FragmentNewsScreenBinding? = null
private val binding get() = _binding!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentNewsScreenBinding.bind(view)
val adapter = NewsArticlesAdapter()
binding.apply {
newsRecyclerView.setHasFixedSize(true)
newsRecyclerView.adapter = adapter
}
viewModel.articles.observe(viewLifecycleOwner) {
adapter.submitData(viewLifecycleOwner.lifecycle, it)
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
您是 运行 没有协程支持的旧版 Retrofit。
要启用对 suspend
函数的支持,请将 Retrofit 升级到版本 2.6.0
或更高版本。