gpt4 book ai didi

android - 获取空值作为响应

转载 作者:行者123 更新时间:2023-12-02 13:28:43 26 4
gpt4 key购买 nike

我正在尝试从 https://www.reddit.com/r/popular/.rss 获得回复并映射到 Android 中的 Kotlin POJO 类。但是当我记录该类别的标签值时,得到空值。对于标题,我得到了作为流行链接的响应值。
这是实体类 FeedX:-

@Root(name = "feed", strict = false)
class FeedX {
@set: Element(name = "category")
@get: Element(name = "category")
var category: Category? = null
val entry: List<Entry>? = null
val id: String? = null
val link: List<LinkX>? = null
@set: Element(name = "title")
@get: Element(name = "title")
var title: String? = null
val updated: String? = null

}
类别类别:-
@Root(name = "category", strict = false)
class Category {
@set: Element(required = false, name = "_label")
@get: Element(required = false, name = "_label")
var _label: String? = null
val _term: String? = null
}
这是 Api 接口(interface):-
interface FeedApi {

@GET("{type}/.rss")
fun getPopularFeeds(
@Path("type") type: String?
): Call<FeedX>?

}
这是主要 Activity :-
class MainActivity : AppCompatActivity() {
private val BASE_URL = "https://www.reddit.com/r/"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build()

val service = retrofit.create(FeedApi::class.java)

service.getPopularFeeds("popular")?.enqueue(object : Callback<FeedX> {
override fun onFailure(call: Call<FeedX>, t: Throwable) {
Log.d("Response Failed", "${t.localizedMessage}")
}

override fun onResponse(call: Call<FeedX>, response: Response<FeedX>) {
if (response.isSuccessful) {
Log.d("Response Success", "${response.body()!!.title}") // for this I am getting value
Log.d("Response Success", "${response.body()!!.category?._label}") // always getting null value
} else {
Log.d("Response Failed jg", "${response.errorBody()}")
}
}
})
}
}

最佳答案

这是因为 title包含一个值,其中 category标签没有。请参阅下面的区别。

<title>popular links</title>
<category term="AskReddit" label="r/AskReddit"/>
如您所见 category标签是自动关闭的。

关于android - 获取空值作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62633227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com