2016-12-30 60 views
-1

我想在一个地址链接,和我用JsoupRecyclerView,所以我这样做:如何解决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

我该如何解决这个错误?

回答

0

使用AsyncTask你的I/O。您无法在主线程上执行联网。

new AsyncTask<Void, Void, Void>(){ 

    public void doInBackground(Void... params){ 
    //I/O here 
    } 
}.execute(); 
相关问题