Android Retrofit 无法解析数据 Rss 提要?
Android Retrofit Unable to parse data Rss feed?
您好,我无法解析数据
这是基础 url https://api.rss2json.com/v1/api.json?rss_url=http://www.abc.net.au/news/feed/51120/rss.xml/
这是我的代码 -->
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
companion object{
val BASEURL = "https://api.rss2json.com/v1/"
rss_url=http://www.abc.net.au/news/feed/"
var retrofitService : RetrofitService? = null
fun getInstance(): RetrofitService{
if (retrofitService ==null){
val retrofit = Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(SimpleXmlConverterFactory.create())
.build()
retrofitService = retrofit.create(RetrofitService::class.java)
}
return retrofitService!!
}
}
和我的存储库
suspend fun getTestFeeds() =
retrofitService.getAllFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")
和我的视图模型
fun getAllFeeds(){
CoroutineScope(Dispatchers.IO).launch {
loading.postValue(true)
val response = mainRepository.getTestFeeds()//getAllFeeds()
withContext(Dispatchers.Main){
if (response.isSuccessful){
feedList.postValue(response.body())
loading.value = false
}else
Log.i(TAG, "getAllFeeds:" +
" ${response.message()} and ${response.body().toString()}" +
" errorbody ${response.errorBody().toString()}")
onError("Error :response.message()}")
}
}
}
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
api 的响应是一个对象,但您需要的是列表。
使用https://www.json2kotlin.com/从json生成数据class并遵循响应结构。应该只是 Response<SomeModel>
而不是 Response<List<Model>>
您请求 returns JsonObject 不是数组,因此它不应该是列表。
{
"status": "ok",
"feed": {
"url": "http://www.abc.net.au/news/feed/51120/rss.xml",
"title": "Just In",
"link": "https://www.abc.net.au/news/justin/",
"author": "",
"description": "",
"image": "https://www.abc.net.au/news/image/8413416-1x1-144x144.png"
},
"items": [{}]
}
所以像下面这样声明你的回复class
class ResponseClass{
val status : String
val feed : YourFeedModel
val items : List<YourItemModel>
}
请求应该是
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<ResponseClass>
首先根据您对 api 的回复,您的回复以对象而非数组开头
那么你的序列化是错误的
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
这个class对象响应
data class ResponseClass(val status : String,val feed : YourFeedModel, val items : List<YourItemModel>)
所以你的函数 getNewTestFeeds
像这样
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) :
Response<ResponseClass>
然后移动存储库文件你使用了错误的方法名称你必须调用这个方法
像这样
suspend fun getTestFeeds() =
retrofitService.getNewTestFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")
然后在 viewModel 上,您想要在实时数据上设置列表
fun getAllFeeds(){
CoroutineScope(Dispatchers.IO).launch {
loading.postValue(true)
val response = mainRepository.getTestFeeds()//getAllFeeds()
withContext(Dispatchers.Main){
if (response.isSuccessful){
feedList.postValue(response.body().items) // response.body() return class response you can get list from class to set it on livedata
loading.value = false
}else
Log.i(TAG, "getAllFeeds:" +
" ${response.message()} and ${response.body().toString()}" +
" errorbody ${response.errorBody().toString()}")
onError("Error :response.message()}")
}
}
}
您好,我无法解析数据 这是基础 url https://api.rss2json.com/v1/api.json?rss_url=http://www.abc.net.au/news/feed/51120/rss.xml/
这是我的代码 -->
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
companion object{
val BASEURL = "https://api.rss2json.com/v1/"
rss_url=http://www.abc.net.au/news/feed/"
var retrofitService : RetrofitService? = null
fun getInstance(): RetrofitService{
if (retrofitService ==null){
val retrofit = Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(SimpleXmlConverterFactory.create())
.build()
retrofitService = retrofit.create(RetrofitService::class.java)
}
return retrofitService!!
}
}
和我的存储库
suspend fun getTestFeeds() =
retrofitService.getAllFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")
和我的视图模型
fun getAllFeeds(){
CoroutineScope(Dispatchers.IO).launch {
loading.postValue(true)
val response = mainRepository.getTestFeeds()//getAllFeeds()
withContext(Dispatchers.Main){
if (response.isSuccessful){
feedList.postValue(response.body())
loading.value = false
}else
Log.i(TAG, "getAllFeeds:" +
" ${response.message()} and ${response.body().toString()}" +
" errorbody ${response.errorBody().toString()}")
onError("Error :response.message()}")
}
}
}
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
api 的响应是一个对象,但您需要的是列表。
使用https://www.json2kotlin.com/从json生成数据class并遵循响应结构。应该只是 Response<SomeModel>
而不是 Response<List<Model>>
您请求 returns JsonObject 不是数组,因此它不应该是列表。
{
"status": "ok",
"feed": {
"url": "http://www.abc.net.au/news/feed/51120/rss.xml",
"title": "Just In",
"link": "https://www.abc.net.au/news/justin/",
"author": "",
"description": "",
"image": "https://www.abc.net.au/news/image/8413416-1x1-144x144.png"
},
"items": [{}]
}
所以像下面这样声明你的回复class
class ResponseClass{
val status : String
val feed : YourFeedModel
val items : List<YourItemModel>
}
请求应该是
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<ResponseClass>
首先根据您对 api 的回复,您的回复以对象而非数组开头 那么你的序列化是错误的
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>
这个class对象响应
data class ResponseClass(val status : String,val feed : YourFeedModel, val items : List<YourItemModel>)
所以你的函数 getNewTestFeeds
像这样
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) :
Response<ResponseClass>
然后移动存储库文件你使用了错误的方法名称你必须调用这个方法 像这样
suspend fun getTestFeeds() =
retrofitService.getNewTestFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")
然后在 viewModel 上,您想要在实时数据上设置列表
fun getAllFeeds(){
CoroutineScope(Dispatchers.IO).launch {
loading.postValue(true)
val response = mainRepository.getTestFeeds()//getAllFeeds()
withContext(Dispatchers.Main){
if (response.isSuccessful){
feedList.postValue(response.body().items) // response.body() return class response you can get list from class to set it on livedata
loading.value = false
}else
Log.i(TAG, "getAllFeeds:" +
" ${response.message()} and ${response.body().toString()}" +
" errorbody ${response.errorBody().toString()}")
onError("Error :response.message()}")
}
}
}