简体   繁体   English

Android Retrofit 无法解析数据 Rss 提要?

[英]Android Retrofit Unable to parse data Rss feed?

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/嗨,我无法在这里解析数据是基础 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()}")
        }
    }
}
@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.来自 api 的响应是 object,但您期待的是列表。

Use https://www.json2kotlin.com/ to generate the data class from the json and follow the response structure.使用https://www.json2kotlin.com/从 json 生成数据 class 并遵循响应结构。 It should be just Response<SomeModel> not Response<List<Model>>它应该只是Response<SomeModel>而不是Response<List<Model>>

You request returns JsonObject not array so it's should not be list.您请求返回 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": [{}]
}

so declare your response class like below所以像下面这样声明你的回复 class

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>

First according on your response from api your response start with object not array then your serializable is wrong首先根据您对 api 的回复,您的回复以 object 开头,而不是数组,那么您的可序列化是错误的

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

this class of object response object 回复中的这个 class

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

so your function getNewTestFeeds like this所以你的 function getNewTestFeeds是这样的

   @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然后在 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()}")
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM