通常,您不应该使用正则表达式来解析 HTML。尝试改用JSoup - 它更实用一些。
获取 jsoup JAR,将其添加到您的项目中并执行以下操作:
class SpotifyTask extends AsyncTask<String, Void, List<String>> {
@Override
protected List<String> doInBackground(String... params) {
ArrayList<String> res = new ArrayList<String>();
try {
Document doc = Jsoup.connect(params[0]).get();
// CSS-style selectors are your new best friend when scraping!
Elements coverArts = doc.select("img[id=cover-art]");
for (Element coverArt : coverArts) {
String url = coverArt.absUrl("src");
if (!TextUtils.isEmpty(url)) {
res.add(url);
}
}
} catch (IOException e) {
Log.w(TAG, "Error processing document", e);
}
return res;
}
@Override
protected void onPostExecute(List<String> result) {
Log.i(TAG, "Do something with your links: " + result);
}
}
在您的活动中像这样开始:
new SpotifyTask().execute("http://open.spotify.com/track/296mPMQavmf1vvxYrUvLN8");