Я пытаюсь разобрать эту страницу через jsoup и получить ArrayList прямых ссылок на mp3 файлы глав книги, но что бы я не пробовал у меня не выходит.
Отдельный элемент
<div class="jp-free-media" style="font-size:xx-small;">(<a id="jp_playlist_1_item_0_mp3" href="http://www.archive.org/download/huckleberry_mfs_librivox/huckleberry_finn_01_twain_64kb.mp3" tabindex="1">download</a>)</div>
Мой класс AsyncTask, строки пытаюсь получить в mLines2:
public class FillBook extends AsyncTask<Void, Void, List<String>> {
//site url to be passed into consructor
private String link;
private String imgLink;
private String title;
String description;
private List<String> tmpChapters = new ArrayList<>();
private List<SingleBook> books = new ArrayList<>();
public FillBook(String link, String imgLink, String title) {
this.link = link;
this.imgLink = imgLink;
this.title = title;
}
@Override
protected List<String> doInBackground(Void... params) {
//parsed doc will be stored in this field
Document doc = null;
//fields to store raw html lines used to extract book names, their thumbnails
// as well as number of total pages of the books category
Elements mLines;
try {
//connect to the site
doc = Jsoup.connect(link).get();
} catch (IOException | RuntimeException e) {
e.printStackTrace();
}
if (doc != null) {
// getting all elements with classname "layout"
mLines = doc.getElementsByClass("book-description");
//searching for book names and their thumbnails and adding them to ArrayLists
for (Element mLine : mLines) {
description = mLine.text();
}
String arr = "";
String html = doc.body().html();
if (html.contains("var audioPlaylist = new Playlist(\"1\", ["))
arr = html.split("var audioPlaylist = new Playlist\\(\"1\", \\[")[1];
if (arr.contains("]"))
arr = arr.split("\\]")[0];
Log.d(LOG_TAG, "ARRAY! " + arr);
//-----------------------------------------
if (arr.contains("}") && arr.contains("{")) {
Log.d(LOG_TAG, "wierd");
for (String mLine2 : arr.split("\\},\\{")) {
if (mLine2.contains("mp3:\"")) {
tmpChapters.add(mLine2.split("mp3:\"")[1].split("\"")[0]);
}
}
} else if (arr.contains("mp3:\""))
Log.d(LOG_TAG, "something whrong");
tmpChapters.add(arr.split("mp3:\"")[1].split("\"")[0]);
}else
System.out.println("ERROR");
Log.d(LOG_TAG, "SIZE_OF_tmpChapters" + tmpChapters.size());
return tmpChapters;
}
protected void onPostExecute(List<String> tmpChapters) {
super.onPostExecute(tmpChapters);
Toast.makeText(BookActivity.this, "size "+ tmpChapters.size(), Toast.LENGTH_SHORT).show();
if (tmpChapters.size() > 0) {
try {
Picasso.get().load(imgLink).into(bookCover);
nameAndAuthor.setText(title);
bookDescription.setText(description);
for (int i = 0; i < tmpChapters.size(); i++) {
books.add(new SingleBook(tmpChapters.get(i)));
}
if (listChapters.getAdapter() != null) {
adapter.clear();
adapter.addAll(books);
} else {
adapter = new CustomAdaterChapters(BookActivity.this,
R.layout.book_chapters_listview_item, books);
listChapters.setAdapter(adapter);
}
listChapters.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String link = adapter.getChapters(position);
playbackIntent = new Intent(BookActivity.this, PlayerService.class);
startService(playbackIntent.putExtra("link", link));
}
});
} catch (RuntimeException e) {
e.printStackTrace();
}
} else Toast.makeText(BookActivity.this, "NETWORK ERROR", Toast.LENGTH_LONG).show();
}
}
В итоге получаю пустой ArrayList
. Посоветуйте, пожалуйста, как запросить с учетом того, что следующая глава будет соответственно id="jp_playlist_1_item_1_mp3"
и так далее.