/** * Check last.fm XML result for error messages. * * @param xml * @return error message or null if everything ok */ public static LastfmError checkXmlTagForErrorMessages(XmlTag xmlTag) { if (xmlTag == null) { Log.e(TAG + "#checkXmlTagForErrorMessages(XmlTag)", "xmlTag is null"); return new LastfmError("error", -1, "Xml result is null!"); } String lastfmStatusValue = xmlTag.getAttribute("status"); if ("ok".equals(lastfmStatusValue)) { /** everything is ok * */ return null; } /** an error happened * */ XmlTag errorTag = xmlTag.getChildByName("error"); String errorCode = errorTag.getAttribute("code"); String errorMessage = errorTag.getValue(); LastfmError error = new LastfmError(lastfmStatusValue, errorCode, errorMessage); if (error != null) { Log.e( TAG + "#checkXmlTagForErrorMessages(XmlTag)", "" + "Lastfm errorCode " + error.getErrorCode() + ": " + error.getErrorMessage()); } return error; }
public static Track getTrackFromXml(XmlTag trackTag) { if (trackTag == null) { return null; } String id = trackTag.getChildByNameValue("id"); String name = trackTag.getChildByNameValue("name"); if (name != null) name = Html.fromHtml(name).toString(); String mbid = trackTag.getChildByNameValue("mbid"); String url = trackTag.getChildByNameValue("url"); boolean streamable = "1".equals(trackTag.getChildByNameValue("streamable")) ? true : false; int trackNumber = 0; int duration = 0; int listeners = 0; int playcount = 0; try { trackNumber = Integer.parseInt(trackTag.getAttribute("rank")); duration = Integer.parseInt(trackTag.getChildByNameValue("duration")); listeners = Integer.parseInt(trackTag.getChildByNameValue("listeners")); playcount = Integer.parseInt(trackTag.getChildByNameValue("playcount")); } catch (NumberFormatException e) { } catch (NullPointerException e) { } Artist artist = getSimilarArtistFromXml(trackTag.getChildByName("artist")); Album album = getAlbumFromXml(trackTag.getChildByName("album")); List<Tag> tags = getTagsFromXml(trackTag.getChildByName("toptags")); Map<String, Object> wiki = getWikiTextFromXml(trackTag.getChildByName("wiki")); Date published = null; String summary = null; String content = null; if (wiki != null) { published = (Date) wiki.get("published"); summary = (String) wiki.get("summary"); content = (String) wiki.get("content"); } Track track = new Track(name, url); track.setId(id); track.setMbid(mbid); track.setStreamable(streamable); track.setTrackNumber(trackNumber); track.setDuration(duration); track.setListeners(listeners); track.setPlaycount(playcount); track.setArtist(artist); track.setAlbum(album); track.setTags(tags); track.setPublished(published); track.setSummary(summary); track.setContent(content); return track; }