I try to parse the site, but the variables title , description , image , link , dateTime , comment empty.

What am I doing wrong?

 public class Article extends AsyncTask<Object, Object, ArrayList<ArticleColumn>> { ArrayList<ArticleColumn> articleColumns = new ArrayList<>(); Elements contentTitle; Elements contentDescription; Elements contentImage; Elements contentLink; Elements contentDateTime; Elements contentComment; Element content; String title; String description; String image; String link; String dateTime; String comment; Document doc = null; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected ArrayList<ArticleColumn> doInBackground(Object... voids) { try { doc = Jsoup.connect("https://dev.by").get(); contentTitle = doc.select(".articleColumn-preview__title"); contentDescription = doc.select(".articleColumn-preview__description"); contentImage = doc.select(".articleColumn-preview__image > img[src]"); contentLink = doc.select(".articleColumn-preview__title__link"); contentDateTime = doc.select(".articleColumn-preview__info__datetime"); contentComment = doc.select(".articleColumn-preview__footer"); for (int i = 0; i < contentTitle.size(); i++) { content = contentTitle.get(i); title = content.text(); content = contentDescription.get(i); description = content.text(); content = contentImage.get(i); image = content.attr("abs:src"); content = contentLink.get(i); link = content.attr("abs:href"); content = contentDateTime.get(i); dateTime = content.text(); content = contentComment.get(i); comment = content.text(); Log.d(TAG, "doInBackground: " + title); articleColumns.add(new ArticleColumn(title, description, dateTime, image, comment, link)); } Log.d(TAG, "doInBackground: articleColumns " + articleColumns); } catch (IOException e) { e.printStackTrace(); } return articleColumns; } @Override protected void onPostExecute(ArrayList<ArticleColumn> stringArrayList) { super.onPostExecute(stringArrayList); recyclerAdapter = new RecyclerAdapter(articleColumns); recyclerView.setAdapter(recyclerAdapter); swipeRefreshLayout.setRefreshing(false); progressDialog.dismiss(); } } 

    1 answer 1

    Your variables are empty , since contentTitle.size() is zero and the loop

     for (int i = 0; i < contentTitle.size(); i++) {...} 

    never done.

    contentTitle.size() is zero, since in the string:

     contentTitle = doc.select(".articleColumn-preview__title"); 

    The select(...) method returns an empty Elements object.

    In turn, the select(...) method returns empty Elements , since there are no elements on the given page that correspond to the .articleColumn-preview__title selector.


    You can get the headlines from the following page:

     Document doc = Jsoup.connect("https://dev.by").get(); Elements contentTitle = doc.select("a.article-preview__title__link"); 

    And Jsoup has such a very useful service - https://try.jsoup.org/ .

    • understand everything, thanks, the error was in the wrong tags - nikolay