-1

I want to get links in a address, and I use Jsoup and RecyclerView, so I do this:

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;

}  

However, I get this error: android.os.NetworkOnMainThreadException!

How can I solve this error?

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Amir Sasani
  • 317
  • 6
  • 19

2 Answers2

0

Please use AsyncTask . You cannot make a network call on UIThread.

https://developer.android.com/reference/android/os/AsyncTask.html

khetanrajesh
  • 300
  • 3
  • 13
0

use an AsyncTask for your I/O. You can't perform networking on the main thread.

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

  public void doInBackground(Void... params){
    //I/O here
  }
}.execute();
BiGGZ
  • 503
  • 4
  • 17