我试图使用Retrofit和Android体系结构组件调用API,但是我得到了这个错误
java.lang.RuntimeException:未能调用没有arg的公共android.arch.lifecycle.LiveData()
这是响应的数据类。
data class ForecastResult(val city: City, val list: List<Forecast>)
服务接口
interface ServicesApis { // using dummy data
@GET("data/2.5/forecast/")
fun getForecast(@Query("APPID") APPID: String = "xxxxxxxx"
, @Query("q") q: String = "94043", @Query("mode") mode: String = "json", @Query("units") units: String = "metric"
, @Query("cnt") cnt: String = "7"): Call<LiveData<ForecastResult>>
}
以及api的实现。
class WeatherRepoImpl : WeatherRepo {
override fun getDailyForecast(): LiveData<Resource<ForecastResult>> {
val forecast: MutableLiveData<Resource<ForecastResult>> = MutableLiveData()
RestAPI.getAPIsrevice().getForecast().enqueue(object : Callback<LiveData<ForecastResult>> {
override fun onResponse(call: Call<LiveData<ForecastResult>>?, response: Response<LiveData<ForecastResult>>?) {
when {
response!!.isSuccessful -> {
forecast.postValue(Resource.success(response.body()?.value))
}
else -> {
val exception = AppException(responseBody = response.errorBody())
forecast.postValue(Resource.error(exception))
}
}
}
override fun onFailure(call: Call<LiveData<ForecastResult>>?, t: Throwable?) {
val exception = AppException(t)
forecast.postValue(Resource.error(exception))
}
})
return forecast
}
}
感谢你的帮助!
发布于 2018-09-08 11:18:19
从API调用中删除LiveData并创建包含在MutableLiveData
对象中的ViewModel
类。
例如:
API调用定义如下:(Remove LiveData)
@GET("data/2.5/forecast/")
fun getForecast(@Query("APPID") APPID: String = "xxxxxxxx"
, @Query("q") q: String = "94043", @Query("mode") mode: String = "json", @Query("units") units: String = "metric"
, @Query("cnt") cnt: String = "7"): Call<ForecastResult>
创建一个ViewModel类:
class YourViewModel: ViewModel() {
var allObjLiveData = MutableLiveData<YOUROBJECT>()
fun methodForAPICall(){
mApiService?.getForecast(.....)?.enqueue(object : Callback<YOUROBJECT>
{
override fun onFailure(call: Call<YOUROBJECT>, t: Throwable) {
allObjLiveData.value = null
}
override fun onResponse(call: Call<YOUROBJECT>, response: Response<YOUROBJECT>) {
allObjLiveData.value=response.body() //Set your Object to live Data
}
})
}
}
现在,在您的活动或片段初始化中,ViewModel类。并观察实时数据。
yourViewModelClassObj?.allObjLiveData?.observe(this, Observer {
//whenever object change this method call every time.
}
})
所以您可以在ViewModel类中使用livedata。
发布于 2018-03-14 16:22:43
是的,所以可能您的API的响应没有被正确序列化。
无论如何,将LiveData
封装到API响应中是没有意义的。只要让你暴露的物体像这样:
someLivedataObject: LiveData<YourApiResponseModel>
// So whenever the APIs response comes in:
someLivedataObject.postValue(YourApiResponseModel)
如果由于某种原因不能工作的话
someLivedataObject.setValue(YourApiResponseModel)
您可以在文档中更多地了解这些方法之间的差异,请注意,我正在调用LiveData#setValue()
方法,不要使用Kotlin的deference方法。
而且,由于您的视图正在观察公开的someLivedataObject
的更改,所以UI将被更新。
这与使用可观察包装器的API响应类似,它不是一个连续的数据流,因此使用RxJava更有意义。
对于这些建议,我并不是完全了解您的应用程序的用例,也有例外--。
发布于 2018-03-18 10:36:08
我使用LiveDataAdapter
将Call
转换为LiveData
,这样就不需要将Retrofit响应包装为Call<LiveData<T>>
。此适配器与杰克的Rx适配器类似。
把LiveDataAdapterFactory
和LiveDataAdapter
从这里那里拿来。
添加了Adapter之后,您需要设置它:
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(LiveDataCallAdapterFactory())
.baseUrl(BASE_URL)
.client(httpClient)
.build()
自我推销:我在同一网站上制作了一段视频:https://caster.io/lessons/android-architecture-components-livedata-with-retrofit
https://stackoverflow.com/questions/49280365
复制相似问题