我想在一個地址鏈接,和我用Jsoup
和RecyclerView
,所以我這樣做:如何解決android.os.networkonmainthreadexception
public static List<News> newsList(String url) {
List<News> newsArrayList = new ArrayList<>();
try {
Document document = Jsoup.connect().get();
Elements newsElements = document.select(".boxMiddle .grpLinks a");
int i = 1;
for (Element newsElement : newsElements) {
News news = new News();
news.setId(i);
news.setTitle(newsElement.text());
news.setDate(newsElement.attr("title"));
news.setUrl(Uri.parse("www.google.com"));
newsArrayList.add(news);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
return newsArrayList;
}
不過,我得到這個錯誤:android.os.NetworkOnMainThreadException
!
我該如何解決這個錯誤?