获得多个Element JSoup Java Android

时间:2019-03-13 00:28:53

标签: java android jsoup

我正在尝试获取项目列表以形成播放列表,并且我只能检索其中一个项目。这是我要进入recyclerview的bindView的代码:

@Override
public void onBindViewHolder(PlaylistViewHolder holder, int position)
{

    try
    {
        String url = "https://www.c895.org/playlist";
        Document document = Jsoup.connect(url).get();

        Element playlist = document.select("#playlist").first();

        List<TrackInfo> tracks = new ArrayList<>();

        for(Element track : playlist.children())
        {
            long time = Long.parseLong(track.dataset().get("ts"));
            String title = track.select(".title").text();
            String artist = track.select(".artist").text();

            tracks.add(new TrackInfo(new Date(time * 1000), title, artist));
        }
        for(int i = 0; i < tracks.size() - 1; i++)
        {

            holder.titlesView.setText(tracks.get(i).toString());
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

}

理想情况下,我想获得10到20个结果。反正我能做到吗?

1 个答案:

答案 0 :(得分:1)

这是因为您所需的html部分在以下标记中:

<div id="playlist">

</div>

因此您不能使用以下内容:

Element playlist = document.select("#playlist").first();

但是您需要使用div#playlist来获取所有播放列表项:

Element playlist = document.select("div#playlist");