Retrofit LiveDataCallAdapter doesn't call function adapt (call)

*爱你&永不变心* 提交于 2020-01-24 13:28:06

问题


Trying to solve this problem about 4 days, help, please. I'm creating an app with rest API (retrofit), try to implement LiveDataCallAdapter from Google samples https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample, but retrofit doesn't call adapter method adapt for getting a response from the server. I'm edited only NetworkBoundResourse (for working without DB) Trying to put breakpoints, after I start repo (login), LiveDataCallAdapter fun adapt (where call.enequeue don't want start) debugging don't call

Here is my piece of code, thx

Providing my service instance

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

There is my LiveDataCallAdapterFactory and 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))
                        }
                    })
                }
            }
        }
    }
}

There is my NetworkBoundResourse (work only with 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>>
}

My Repo class


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

And my AuthResponse Class


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
}

My ViewModel class from where i start calling


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:


So guys, finaly i found a solution. It's very simple for the peaple experienced in MVVM (live data) subject , but im beginer in MVVM and my brain exploded while I came to this. So , the problem was I subscribed to Repo livedata from ViewModel , not from View (Fragment in my case). After i locked the chain of livedata observers View - ViewModel - Repo - Service - everything worked. Thx to all



来源:https://stackoverflow.com/questions/55764251/retrofit-livedatacalladapter-doesnt-call-function-adapt-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!