Home > Back-end >  Android Retrofit Unable to parse data Rss feed?
Android Retrofit Unable to parse data Rss feed?

Time:02-25

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()}")
            one rror("Error :response.message()}")
        }
    }
}

CodePudding user response:

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>

CodePudding user 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>>

  • Related