How to get album art from last.fm Android - android

I'm making an Music player for Android, I want to provide feature for users to get album art of a song from last.fm.
I've got my API key too. Just need help for retrieving the image from Last.fm.
Any help in getting the image url would also be appreciated.
Thanks in advance.
P.S : For more info about my music player, check the link below
https://plus.google.com/u/0/communities/115046175816530349000

I found an solution check below
Add the below AsyncTask loader
public class RetrieveFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
String albumArtUrl = null;
try {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(urls[0]); // getting XML from URL
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("image");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
Log.d(LOG_TAG,"Size = " + e.getAttribute("size") + " = " + parser.getElementValue(e));
if(e.getAttribute("size").contentEquals("medium")){
albumArtUrl = parser.getElementValue(e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return albumArtUrl;
}
}
Call it as followed :
StringBuilder stringBuilder = new StringBuilder("http://ws.audioscrobbler.com/2.0/");
stringBuilder.append("?method=album.getinfo");
stringBuilder.append("&api_key=");
stringBuilder.append("YOUR_LAST_FM_API_KEY");
stringBuilder.append("&artist=" + URLEncoder.encode("ARTIST_NAME_HERE", "UTF-8"));
stringBuilder.append("&album=" + URLEncoder.encode("ALBUM_NAME_HERE", "UTF-8"));
url = new RetrieveFeedTask().execute(stringBuilder.toString()).get();
You need 2 classes :
1. XmlParser
2. DocElement
Both of which will be available in link below.
Xml parsing tutorial

Please see Last.fm Web Services docs for album.getInfo: http://www.last.fm/api/show/album.getInfo
Here is a sample response, from which you can easily see how to get cover art image url:
<album>
<name>Believe</name>
<artist>Cher</artist>
<id>2026126</id>
<mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid>
<url>http://www.last.fm/music/Cher/Believe</url>
<releasedate>6 Apr 1999, 00:00</releasedate>
<image size="small">...</image>
<image size="medium">...</image>
<image size="large">...</image>
<listeners>47602</listeners>
<playcount>212991</playcount>
<toptags>
<tag>
<name>pop</name>
<url>http://www.last.fm/tag/pop</url>
</tag>
...
</toptags>
<tracks>
<track rank="1">
<name>Believe</name>
<duration>239</duration>
<mbid/>
<url>http://www.last.fm/music/Cher/_/Believe</url>
<streamable fulltrack="0">1</streamable>
<artist>
<name>Cher</name>
<mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
<url>http://www.last.fm/music/Cher</url>
</artist>
</track>
...
</tracks>
</album>

Related

Android Jsoup Table Index out of Bounds

When I try to parse the document from a html file it works fine.
But, when I try to parse a document from a url it gives the following error:
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
I am sure the content from the file is the same from the url and I also tried using threads
Here, below is the website:
http://pucminas.br/relatorio_atividades_2014/arquivos/ensino_graduacao.htm
Here, below, is the code
class MyTask extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
String title ="";
try {
URL url = new URL(getString(R.string.url));
Document doc = Jsoup.parse(url, 3000);
Element table = doc.select("table").get(3);
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
}
You should know that Jsoup has a size limit and a timeout limit also, therefor not every table is parsed.
Fortunately, there's a way to change this when connecting to the site and making your document object.
Solution
Document doc = Jsoup.connect(url).maxBodySize(0)
.timeout(0)
.followRedirects(true)
.get();
JSoup APIDocs
Connection#maxBodySize(int bytes)
Update the maximum body size, in bytes.
Connection#timeout(int millis)
Update the request timeout.

Empty String on opening self closed tag from rss feed

I've to read a stream from a rss feed on an Android application.
All work fine, but i'm not able to get the complete url, from the tag, because it's a selfclosed tag
somethings like
This's the xml page (i can't edit it) Xml source page
and this is the code to populate to create the single objet that I need
String titolo, descrizione, descrizione_breve, img, data, icona;
Notizia SitoDaAggiungere;
for (int i = 0; i < nodi.getLength(); i++) {
Node nodoItem = nodi.item(i);
if (nodoItem.getNodeType() == Node.ELEMENT_NODE) {
Element elemento = (Element) nodoItem;
titolo = elemento.getElementsByTagName("title").item(0).getTextContent();
descrizione_breve = elemento.getElementsByTagName("summary").item(0).getTextContent();
descrizione = elemento.getElementsByTagName("content").item(0).getTextContent();
img = elemento.getElementsByTagName("pic1").item(0).getTextContent();
data = elemento.getElementsByTagName("updated").item(0).getTextContent();
icona = elemento.getElementsByTagName("pic").item(0).getTextContent();
String link_sito = elemento.getElementsByTagName("link").item(0).getTextContent(); // <-- no error, but an empty string
SitoDaAggiungere = new Notizia(titolo, descrizione, descrizione_breve, data, img, icona, link_sito);
InserisciSito(SitoDaAggiungere);
}
}
Someone can help me?
thank's a lot!
finally I do it!!!
this's the code to get the url
String link_sito = elemento.getElementsByTagName("link").item(0).getAttributes().item(0).getTextContent();
now I simply use it to create the new objet

Encoding issue on Kit Kat / Android when saving and reading a webview

I have noticed that for Android 4.4 handsets, saving a webview with:
webview.saveWebArchive(name);
and reading it after with WebArchiveReader WebArchiveReader (see code below) throws an Encoding Exception:
11-08 15:10:31.976: W/System.err(2240): org.xml.sax.SAXParseException: Unexpected end of document
11-08 15:10:31.976: W/System.err(2240): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:125)
The method used to read the stored XML file worked perfectly fine until 4.3 and it is (NOTE: I tried to parse it in two different ways):
public boolean readWebArchive(InputStream is) {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
myDoc = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
try {
//New attempt
InputSource input = new InputSource(is);
input.setEncoding("UTF-8");
myDoc = builder.parse(input);
//This used to be the way it used to work for
//Android 4.3 and below without trouble
//myDoc = builder.parse(is);
NodeList nl = myDoc.getElementsByTagName("url");
for (int i = 0; i < nl.getLength(); i++) {
Node nd = nl.item(i);
if(nd instanceof Element) {
Element el = (Element) nd;
// siblings of el (url) are: mimeType, textEncoding, frameName, data
NodeList nodes = el.getChildNodes();
for (int j = 0; j < nodes.getLength(); j++) {
Node node = nodes.item(j);
if (node instanceof Text) {
String dt = ((Text)node).getData();
byte[] b = Base64.decode(dt, Base64.DEFAULT);
dt = new String(b);
urlList.add(dt);
urlNodes.add((Element) el.getParentNode());
}
}
}
}
} catch (SAXParseException se){
//Some problems parsing the saved XML file
se.printStackTrace();
myDoc = null;
} catch (Exception e) {
e.printStackTrace();
myDoc = null;
}
return myDoc != null;
}
I've played a bit with the way the buider is invoked. Instead of giving it a FileInputStream, I first create an InputSource as you can see to force a given encoding. However, I had no success. By not including the InputSource, the exception was instead:
org.xml.SAXParseException: Unexpected token
I've read in previous posts that this may be an encoding issue (e.g. android-utf-8-file-parsing) but none of the proposed solutions worked for me.
Does anyone else have the same issue or does anyone know what has changed on Kit Kat, and if so, how could it be avoided?
Many thanks in advance
My WebArchiveReader code is not needed under Android 4.4 KitKat and newer to read back a saved web archive. If you save your page with webview.saveWebArchive(name); method on KitKat, you get an MHTML formatted file, as "#Dragon warrior" indicates above. To read this file back into webview, just use:
webView.loadUrl("file:///my_folder/mySavedPage.mht");
Just make sure to give your file the .mht or .mhtml extension, so that WebView recognizes its contents. Otherwise it may just display the MHTML code in text format.
Greg
I have the exactly same problem as you do.
Apparently, Android 4.4 WebView saves web archives as MHTML. Therefore, you can't use WebArchiveReader anymore.
You might want to parse MHTML files with some other 3rd party lib. Good luck!

parsing does not working in android

I am parsing a xml from an url.The url is has mobile IMEI no and searchstring based on my application. i put my xml parsing code in android project it does not work. but if i run as separate java program it is working. please help me.
Log.e("rsport-", "function1");
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // Convert CDATA to Text nodes
factory.setNamespaceAware(false); // No namespaces: this is default
factory.setValidating(false); // Don't validate DTD: also default
DocumentBuilder parser = factory.newDocumentBuilder();
Log.e("rsport-", "function2");
Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a");
Log.e("rsport-","function3");
NodeList sections = document.getElementsByTagName("Searchdata");
int numSections = sections.getLength();
for (int i = 0; i < numSections; i++)
{
Element section = (Element) sections.item(i);
if(section.hasChildNodes()==true){
NodeList section1=section.getChildNodes();
for(int j=0;j<section1.getLength();j++){
if(section1.item(j).hasChildNodes()==true) {
for(int k=0;k<section1.item(j).getChildNodes().getLength();k++)
xmlvalue=String.valueOf(section1.item(j).getChildNodes().item(k).getNodeValue()).trim();
arl.add(xmlvalue);
}
}
}
}
}
}
catch(Exception e){}
System.out.println("id"+id+" searchdatacount"+searchdatacount);
System.out.println("---------");
ListIterator<String> litr = arl.listIterator();
while (litr.hasNext()) {
String element = litr.next();
Log.e("rsport-", "elememt");
}
after the Log.e("rsport-", "function2"); does not work.
Refer my blog, i had gave Detailed explanation, http://sankarganesh-info-exchange.blogspot.com/2011/04/parsing-data-from-internet-and-creating.html, and make sure , that you had add the Internet permission in your Manifest file.
If you had gone through Myblog, then you will able to notice that you did the following line as wrong
Document document = parser.parse("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a");
use like this
URL url =new URL("http://demo.greatinnovus.com/restingspot/search?userid=xxxxxxxxxxxxxxxx&firstname=a&lastname=a");
Document document= parser.parse(new InputSource(url.openStream()));

Using DOM parser in Android

I'm using the DOM parser to retrive information from a XML file that looks like this:
<data>
<metData>
<wantedInformation>
</metData>
<metData>
<Information>
</metData>
<metData>
<Information>
</metData>
<data>
The problem is because I don't know how to parse only the first part of <metData>. I don't need the second and the third part, but the parser displays them anyway.
The xml file is from a weather forcast site:
http://www.meteo.si/uploads/probase/www/fproduct/text/sl/fcast_SLOVENIA_MIDDLE_latest.xml
and I need just the following line: <nn_shortText>oblačno</nn_shortText>
Pls take care whether your XML file is well formed or not,
You have to the notice three methods which i had shown below, they are
1. getElementsByTagName - Mention the tag which you want to parse
2.getChildNodes - retervies the child node
3.getNodeValue()- with the help of this method you can access the
value of particular tag
Step 1: Create a Method to parse _Information_Value ,inorder to parse the data of Information tag
String[] infoId=null;
public void parse_Information_Value() throws UnknownHostException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.getInputStream());
org.w3c.dom.Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("metData");
int a=items.getLength();
int k=0;
for (int i = 0; i < items.getLength(); i++) {
Message_category message = new Message_category();
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("wantedInformation")) {
message.setId(property.getFirstChild()
.getNodeValue());
infoId[k]=property.getFirstChild().getNodeValue();
k++;
}
}
}
} catch (Exception e) { }
}
Depending on the size of your document, you may also want to use at a streaming oriented parser like SAX or Stax, which does not pull the whole document into memory and thus needs less memory than DOM.
Good thing is that SAX is already built into Android, so you can use it right away.
See this link for a usage example.

Categories

Resources