用andoid studio编写一个简单的新闻列表和新闻详细页的app。但是现在数据加载错误。点击一直显示加载失败,报错显示没有成功创建转化器

数据是网络爬虫的,用了retrofit和JSoup。
不知道哪里出错为什么不能创建转化器。
代码
数据类
NewsItem
data class NewsItem (
val title: String,
val content: String,
val source:String,
val dateTime: String,
val url: String,
val imgSrc: String,
)
NewsList
package com.example.newsapp.model
class NewsList:ArrayList<NewsItem>()
NewsApi
package com.example.newsapp.api
import com.example.newsapp.model.NewsItem
import com.example.newsapp.model.NewsList
import com.example.newsapp.model.NewsPage
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
//https://www.cuc.edu.cn/news/1901/list.htm中传要闻
//https://www.cuc.edu.cn/news/10127/list.htm科学研究
//https://www.cuc.edu.cn/news/10128/list.htm今日推荐
interface NewsApi {
//爬取新闻列表页
@GET("news/{title}/list.htm")
suspend fun getHtml(@Path("title")title:String): Call<NewsList>
//爬取新闻详细页
@GET("{url}")
suspend fun getNewsContent(@Path("url")url:String): Call<NewsPage>
}
MyRetrofit
package com.example.newsapp
import com.example.newsapp.api.NewsApi
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object MyRetrofit {
private const val BASE_URL = "https://www.cuc.edu.cn/"
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(NewsConverterFactory())//设计数据解析器
//.addConverterFactory(GsonConverterFactory.create())
.build()
}
val api: NewsApi by lazy {
retrofit.create(NewsApi::class.java)
}
}
NewsConverFactory
package com.example.newsapp
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import com.example.newsapp.model.NewsItem
import com.example.newsapp.model.NewsList
import com.example.newsapp.model.NewsPage
import com.google.gson.reflect.TypeToken
import okhttp3.ResponseBody
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type
class NewsConverterFactory : Converter.Factory() {
override fun responseBodyConverter(
type: Type,
annotations: Array<out Annotation>,
retrofit: Retrofit
): Converter<ResponseBody, *>? {
if (TypeToken.get(type).equals(TypeToken.get(String::class.java)) ){
return HtmlConverter()
}
if (TypeToken.get(type).equals(TypeToken.get(NewsList::class.java))) {
return NewsConverter()
}
if (TypeToken.get(type).equals(TypeToken.get(NewsPage::class.java))) {
return ContentConverter()
}
return null
}
class NewsConverter: Converter<ResponseBody, List<NewsItem>> {
override fun convert(value: ResponseBody):List<NewsItem>? {
val html =value.string()
val newsItems = mutableListOf<NewsItem>()
// val newsList = mutableListOf<NewsItem>()
val doc:Document=Jsoup.parse(html)
val newsElements: Elements = doc.select("ul.news_list li.news")
for (newsElement in newsElements) {
val imageUrl = newsElement.select("div.news_imgs img").attr("src")
val newsUrl = newsElement.select("a").attr("href").removePrefix("/")
val title = newsElement.select("div.news_title").text()
val text = newsElement.select("div.news_text").text()
val publisher = newsElement.select("span.publisher").text()
val date = newsElement.select("span.news_date").text()
newsItems.add(
NewsItem(
title = title,
content = text,
source = publisher,
dateTime = date,
url = newsUrl,
imgSrc = imageUrl
)
)
}
return newsItems
}
}
class ContentConverter:Converter<ResponseBody, NewsPage>
{
override fun convert(value: ResponseBody): NewsPage? {
val html =value.string()
val doc:Document=Jsoup.parse(html)
//提取标题
val titleElement:Element? = doc.select("h1.arti_title ").first()
val title =titleElement?.text()?:""
//提取来源
val sourceElement: Element? = doc.select("span.arti_publisher").first()
val source = sourceElement?.text() ?: ""
//提取浏览次数
val viewCountElement: Element? = doc.select("span.arti_viwe span.WP_VisitCount").first()
val viewCount= viewCountElement?.text() ?: ""
//提取发布时间
val dateTimeElement: Element? = doc.select("span.arti_update").first()
val dateTime = dateTimeElement?.text() ?: ""
//提取正文
val contentElements: Elements = doc.select("p")
var content = ""
for(contentElement in contentElements) {
content += contentElement.text() + '\n';
}
return NewsPage(title,source,viewCount,dateTime,content)
}
}
class HtmlConverter : Converter<ResponseBody, String> {
override fun convert(value: ResponseBody): String? = value.string()
}
}