Kotlin Json解析MVVM [英] Kotlin Json parsing MVVM
问题描述
我正在尝试使用协程将Json解析为MVVM中的Recyclerview来学习MVVM体系结构.但是我在BlogRepository类上遇到错误.
I'm trying to learn MVVM architecture on parse Json into a Recyclerview in MVVM using coroutines. But I'm getting error on BlogRepository class.
我的Json文件如下:
My Json file looks like this:
[
{
"id": 1,
"name": "potter",
"img": "https://images.example.com/potter.jpg"
},
{ …}
]
我已经创建了如下数据类:
I have created data class as below:
@JsonClass(generateAdapter = true)
class ABCCharacters (
@Json(name = "id") val char_id: Int,
@Json(name = "name") val name: String? = null,
@Json(name = "img") val img: String
)
然后如下所示的RestApiService:
Then the RestApiService as below:
interface RestApiService {
@GET("/api")
fun getPopularBlog(): Deferred<List<ABCCharacters>>
companion object {
fun createCorService(): RestApiService {
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build()
return Retrofit.Builder()
.baseUrl("https://example.com")
.addConverterFactory(MoshiConverterFactory.create())
.client(okHttpClient)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build().create(RestApiService::class.java)
}
}
}
BlogReposity.kt
BlogReposity.kt
class BlogRepository() {
private var character = mutableListOf<ABCCharacters>()
private var mutableLiveData = MutableLiveData<List<ABCCharacters>>()
val completableJob = Job()
private val coroutineScope = CoroutineScope(Dispatchers.IO + completableJob)
private val thisApiCorService by lazy {
RestApiService.createCorService()
}
fun getMutableLiveData():MutableLiveData<List<ABCCharacters>> {
coroutineScope.launch {
val request = thisApiCorService.getPopularBlog()
withContext(Dispatchers.Main) {
try {
val response = request.await()
val mBlogWrapper = response;
if (mBlogWrapper != null && mBlogWrapper.name != null) {
character = mBlogWrapper.name as MutableList<ABCCharacters>
mutableLiveData.value=character;
}
} catch (e: HttpException) {
// Log exception //
} catch (e: Throwable) {
// Log error //)
}
}
}
return mutableLiveData;
}
}
最后是ViewModel类
Finally the ViewModel class
class MainViewModel() : ViewModel() {
val characterRepository= BlogRepository()
val allBlog: LiveData<List<ABCCharacters>> get() = characterRepository.getMutableLiveData()
override fun onCleared() {
super.onCleared()
characterRepository.completableJob.cancel()
}
}
I've done this based on https://itnext.io/kotlin-wrapping-your-head-around-livedata-mutablelivedata-coroutine-networking-and-viewmodel-b552c3a74eec Someone can guide me where am I going wrong & how to fix it?
推荐答案
您的getPopularBlog()
API返回List<ABCCharacters>
而不是ABCCharacters
.因此,您不能直接从响应中访问ABCCharacters
的属性.这就是为什么name
属性在此显示Unresolved reference
的原因.
Your getPopularBlog()
API return List<ABCCharacters>
instead of ABCCharacters
. So, you can't access ABCCharacters
's property from your response directly. That's why name
property here shows Unresolved reference
.
尝试以下代码:
if (mBlogWrapper != null && mBlogWrapper.isNotEmpty()) {
character = mBlogWrapper as MutableList<ABCCharacters>
mutableLiveData.value = character
}
这篇关于Kotlin Json解析MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!