Android Retrofit 无法解析数据 Rss feed?

发布于 2025-01-09 20:01:21 字数 1820 浏览 4 评论 0 原文

您好,我无法解析数据 这是基本网址 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()}")
        }
    }
}

Hi im unable to parse data
here is base url https://api.rss2json.com/v1/api.json?rss_url=http://www.abc.net.au/news/feed/51120/rss.xml/

here is my code -->

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

and my repository

suspend fun  getTestFeeds() = 
retrofitService.getAllFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")

and my 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())
                loading.value = false
            }else
                Log.i(TAG, "getAllFeeds:" +
                        " ${response.message()} and ${response.body().toString()}" +
                        " errorbody ${response.errorBody().toString()}")
            onError("Error :response.message()}")
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2025-01-16 20:01:21

您请求返回 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 ResponseClass{

val status : String
val feed : YourFeedModel 
val items : List<YourItemModel>
}

并且请求应该是

@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<ResponseClass>

You request returns JsonObject not array so it's should not be list.

{
    "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": [{}]
}

so declare your response class like below

class ResponseClass{

val status : String
val feed : YourFeedModel 
val items : List<YourItemModel>
}

And request should be

@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<ResponseClass>
荭秂 2025-01-16 20:01:21
@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>

api 的响应是一个对象,但您需要的是列表。

使用 https://www.json2kotlin.com/ 从 json 生成数据类并遵循响应结构。它应该只是 Response 而不是 Response>

@GET("/api.json")
fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>

The response from the api is an object but you are expecting the list.

Use https://www.json2kotlin.com/ to generate the data class from the json and follow the response structure. It should be just Response<SomeModel> not Response<List<Model>>

无声情话 2025-01-16 20:01:21

首先根据 api 的响应,您的响应以对象而不是数组开始
那么您的序列化是错误的

  @GET("/api.json")
 fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>

此类对象响应

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

First according on your response from api your response start with object not array
then your serializable is wrong

  @GET("/api.json")
 fun getNewTestFeeds(@Query( "rss_url") query :String) : Response<List<Model>>

this class of object response

data class ResponseClass(val status : String,val feed : YourFeedModel,   val items : List<YourItemModel>)

so your function getNewTestFeeds like this

   @GET("/api.json")
   fun getNewTestFeeds(@Query( "rss_url") query :String) : 
   Response<ResponseClass>

then move repository file you use wrong name of method you must call this method
like this

suspend fun  getTestFeeds() = 
retrofitService.getNewTestFeeds("http://www.abc.net.au/news/feed/51120/rss.xml")

then on viewModel you want set list on live data

 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()}")
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文