gpt4 book ai didi

android - Retrofit LiveDataCallAdapter 不调用函数 adapt(call)

转载 作者:行者123 更新时间:2023-11-29 02:20:53 26 4
gpt4 key购买 nike

试图解决这个问题大约 4 天,请帮助。我正在使用 rest API(改造)创建一个应用程序,尝试从 Google 示例中实现 LiveDataCallAdapter https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample ,但改造不会调用适配器方法 adapt 以从服务器获取响应。我只编辑了 NetworkBoundResourse(用于在没有 DB 的情况下工作)尝试放置断点,在我开始 repo (登录)后,LiveDataCallAdapter 有趣地适应(其中 call.enequeue 不想开始)调试不调用

这是我的代码,thx

提供我的服务实例

@Singleton
@Provides
fun provideRetrofit(): BrizSmartTVService {
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(LiveDataCallAdapterFactory())
.build()
.create(BrizSmartTVService::class.java)
}

有我的 LiveDataCallAdapterFactory 和 LiveDataCallAdapter

class LiveDataCallAdapterFactory : Factory() {
override fun get(
returnType: Type,
annotations: Array<Annotation>,
retrofit: Retrofit
): CallAdapter<*, *>? {
if (getRawType(returnType) != LiveData::class.java) {
return null
}
val observableType = getParameterUpperBound(0, returnType as ParameterizedType)
val rawObservableType = getRawType(observableType)
if (rawObservableType != ApiResponse::class.java) {
throw IllegalArgumentException("type must be a resource")
}
if (observableType !is ParameterizedType) {
throw IllegalArgumentException("resource must be parameterized")
}
val bodyType = getParameterUpperBound(0, observableType)
return LiveDataCallAdapter<Any>(bodyType)
}
}

class LiveDataCallAdapter<R>(private val responseType: Type) :
CallAdapter<R, LiveData<ApiResponse<R>>> {

override fun responseType() = responseType

override fun adapt(call: Call<R>): LiveData<ApiResponse<R>> {
return object : LiveData<ApiResponse<R>>() {
private var started = AtomicBoolean(false)
override fun onActive() {
super.onActive()
if (started.compareAndSet(false, true)) {
Log.d("TAG", ": onActive Started ");
call.enqueue(object : Callback<R> {
override fun onResponse(call: Call<R>, response: Response<R>) {
Log.d("TAG", ": $response");
postValue(ApiResponse.create(response))
}

override fun onFailure(call: Call<R>, throwable: Throwable) {
Log.d("TAG", ": ${throwable.localizedMessage}");
postValue(ApiResponse.create(throwable))
}
})
}
}
}
}
}

有我的 NetworkBoundResourse(仅适用于网络)


abstract class NetworkBoundResource<RequestType> {

private val result = MediatorLiveData<Resource<RequestType>>()

init {
setValue(Resource.loading(null))
fetchFromNetwork()
}

@MainThread
private fun setValue(newValue: Resource<RequestType>) {
if (result.value != newValue) {
result.value = newValue
}
}

private fun fetchFromNetwork() {
val apiResponse = createCall()
result.addSource(apiResponse) { response ->
result.removeSource(apiResponse)

when (response) {
is ApiSuccessResponse -> {
setValue(Resource.success(processResponse(response)))
}

is ApiErrorResponse -> {
onFetchFailed()
setValue(Resource.error(response.errorMessage, null))

}
}
}
}

protected fun onFetchFailed() {
}

fun asLiveData() = result as LiveData<Resource<RequestType>>

@WorkerThread
protected open fun processResponse(response: ApiSuccessResponse<RequestType>) = response.body

@MainThread
protected abstract fun createCall(): LiveData<ApiResponse<RequestType>>
}

我的 Repo 类


@Singleton
class AuthApiRepo @Inject constructor(
val apiService: BrizSmartTVService
) {

fun authLoginPass(login: String, password: String): LiveData<Resource<AuthResponse>> {
return object : NetworkBoundResource<AuthResponse>() {

override fun createCall(): LiveData<ApiResponse<AuthResponse>> {
val authLogPassBody = AuthLogPassBody(login,password,"password")
Log.d("TAG", ": $authLogPassBody");
return apiService.authLoginPass(authLogPassBody)
}

}.asLiveData()
}
}

还有我的 AuthResponse 类


class AuthResponse {
@SerializedName("token_type")
var tokenType: String? = null
@SerializedName("access_token")
var accessToken: String? = null
@SerializedName("refresh_token")
var refreshToken: String? = null
@SerializedName("user_id")
var userId: String? = null
@SerializedName("expires_in")
var expiresIn: Long = 0
@SerializedName("portal_url")
var portalUrl: String? = null
}

我开始调用的 ViewModel 类


class AuthViewModel @Inject constructor(private val authApiRepo: AuthApiRepo) : ViewModel() {

private var _isSigned = MutableLiveData<Boolean>()
val isSigned: LiveData<Boolean>
get() = _isSigned


fun signIn(login: String, password: String) {
authApiRepo.authLoginPass(login, password)
val authRespons = authApiRepo.authLoginPass(login, password)
Log.d("TAG", ": " + authRespons.value.toString());
//here will by always data null and status LOADING
}

override fun onCleared() {
super.onCleared()
}
}

最佳答案

伙计们,我终于找到了解决方案。对于在 MVVM(实时数据)主题中有经验的人来说,这非常简单,但我是 MVVM 的初学者,当我来到这里时,我的大脑爆炸了。所以,问题是我从 ViewModel 订阅了 Repo livedata,而不是从 View(在我的例子中是 Fragment)。在我锁定实时数据观察者链之后,View - ViewModel - Repo - Service - 一切正常。谢谢大家

关于android - Retrofit LiveDataCallAdapter 不调用函数 adapt(call),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55764251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com