Retrofit LiveDataCallAdapter不调用函数Adapt(调用)

时间:2019-04-19 15:33:55

标签: android kotlin retrofit android-livedata

尝试解决此问题大约4天,请提供帮助。 我正在使用Rest API(改造)创建应用,请尝试从Google示例中实施LiveDataCallAdapter https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample,但改型不会调用适配器方法来适应从服务器获取响应。 我只编辑了NetworkBoundResourse(用于不使用DB的情况) 尝试设置断点,在我开始回购(登录)之后,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(仅适用于Network)


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>>
}

我的回购课程


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

1 个答案:

答案 0 :(得分:0)

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