Форум для тех, кто программирует под Android
-
Anonymous
Получение исключения во время выполнения при анализе RSS-канала
Сообщение
Anonymous »
URL-адрес RSS-канала —
https://feeds.simplecast.com/qm_9xx0g
Функция класса модели My View
Код: Выделить всё
fun getXmlRequest(url: String): LiveData {
isLoading.postValue(true)
val liveData = MutableLiveData()
val coreAdXMLService = retrofitXML?.client?.create(CoreAdXmlService::class.java)
coreAdXMLService?.loadNewsResponse(url)?.enqueue(object : Callback {
override fun onFailure(call: Call, t: Throwable) {
isLoading.postValue(false)
liveData.postValue(emptyList())
}
override fun onResponse(call: Call, response: Response) {
val responseBody: NewsFeedResponseModel? = response.body()
responseBody?.channel?.item?.let {
val list = mutableListOf()
it.forEachIndexed { index, it ->
val newsXmlCustomModel = NewsXmlCustomModel()
newsXmlCustomModel.type = ""
newsXmlCustomModel.title = it.title
newsXmlCustomModel.pos = (index % 2).toString()
newsXmlCustomModel.pubDate = it.pubDate
newsXmlCustomModel.link = it.link
newsXmlCustomModel.description = it.description
newsXmlCustomModel.descriptionLink = it.link
val xml = "${it.description}"
val doc: Document? =
try {
DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(InputSource(StringReader(xml)))
} catch (e: java.lang.Exception) {
logReport(e.message)
null
}
val errNodes: NodeList? = doc?.getElementsByTagName("root")
val node: Node? = errNodes?.item(0)
if (node != null) {
if (node.hasChildNodes()) {
val child: Node? = node.firstChild
val value: String? = child?.childNodes?.item(0)?.nodeValue?.let {
it.replace("".toRegex(), "")
} ?: run {
it.description?.replace("".toRegex(), "")
}
newsXmlCustomModel.desc = value
}
} else {
val pattern: Pattern = Pattern.compile("
(.+?)
", Pattern.CASE_INSENSITIVE)
val matcher: Matcher = pattern.matcher(it.description)
matcher.find()
newsXmlCustomModel.desc =
try {
matcher.group(1)
} catch (e: Exception) {
it.description?.replace("".toRegex(), "") ?: ""
}
}
var imageUrl = ""
if (it.description?.contains("
Подробнее здесь: [url]https://stackoverflow.com/questions/78415446/getting-runtime-exception-while-parsing-rss-feed[/url]
1714590402
Anonymous
URL-адрес RSS-канала — https://feeds.simplecast.com/qm_9xx0g
Функция класса модели My View
[code]fun getXmlRequest(url: String): LiveData {
isLoading.postValue(true)
val liveData = MutableLiveData()
val coreAdXMLService = retrofitXML?.client?.create(CoreAdXmlService::class.java)
coreAdXMLService?.loadNewsResponse(url)?.enqueue(object : Callback {
override fun onFailure(call: Call, t: Throwable) {
isLoading.postValue(false)
liveData.postValue(emptyList())
}
override fun onResponse(call: Call, response: Response) {
val responseBody: NewsFeedResponseModel? = response.body()
responseBody?.channel?.item?.let {
val list = mutableListOf()
it.forEachIndexed { index, it ->
val newsXmlCustomModel = NewsXmlCustomModel()
newsXmlCustomModel.type = ""
newsXmlCustomModel.title = it.title
newsXmlCustomModel.pos = (index % 2).toString()
newsXmlCustomModel.pubDate = it.pubDate
newsXmlCustomModel.link = it.link
newsXmlCustomModel.description = it.description
newsXmlCustomModel.descriptionLink = it.link
val xml = "${it.description}"
val doc: Document? =
try {
DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(InputSource(StringReader(xml)))
} catch (e: java.lang.Exception) {
logReport(e.message)
null
}
val errNodes: NodeList? = doc?.getElementsByTagName("root")
val node: Node? = errNodes?.item(0)
if (node != null) {
if (node.hasChildNodes()) {
val child: Node? = node.firstChild
val value: String? = child?.childNodes?.item(0)?.nodeValue?.let {
it.replace("".toRegex(), "")
} ?: run {
it.description?.replace("".toRegex(), "")
}
newsXmlCustomModel.desc = value
}
} else {
val pattern: Pattern = Pattern.compile("
(.+?)
", Pattern.CASE_INSENSITIVE)
val matcher: Matcher = pattern.matcher(it.description)
matcher.find()
newsXmlCustomModel.desc =
try {
matcher.group(1)
} catch (e: Exception) {
it.description?.replace("".toRegex(), "") ?: ""
}
}
var imageUrl = ""
if (it.description?.contains("
Подробнее здесь: [url]https://stackoverflow.com/questions/78415446/getting-runtime-exception-while-parsing-rss-feed[/url]