Retrieve XML from URL
Posted
by
Pl4za
on Stack Overflow
See other posts from Stack Overflow
or by Pl4za
Published on 2012-09-26T15:26:54Z
Indexed on
2012/09/28
21:37 UTC
Read the original article
Hit count: 269
I am having some problems retrieving a xml from url with the following code:
private static String getAlbumArt(String artistName, String albumName){
try{
XMLParser xml_parser = new XMLParser();
String xml = xml_parser.getXmlFromUrl(getAlbumURL(artistName, albumName));
Document doc = xml_parser.getDomElement(xml);
NodeList N = doc.getElementsByTagName("album");
Node node = N.item(0);
NodeList N2 = node.getChildNodes();
System.out.println("1------");
for (int i = 0; i < N2.getLength(); i++) {
Node detailNode = N2.item(i);
if (detailNode.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("2------");
if (detailNode.getNodeName().equalsIgnoreCase("image"))
{
String sizeVal = ((Element) detailNode).getAttribute("size");
String url = detailNode.getTextContent();
if (sizeVal.equalsIgnoreCase("large")) {
return url;
}
}
}
}
}
catch (Exception e){
}
return null;
}
The xml function which i call in the above code:
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
getAlbumURL:
public static String getAlbumURL(String artist, String album){
return URL_METHOD + METHOD_GETALBUM + AMPERSAND
+ API_KEY + AMPERSAND
+ PARAM_ARTIST + artist + AMPERSAND
+ PARAM_ALBUM + album;
}
XMLparser:
public class XMLParser {
// constructor
public XMLParser() {
}
//Get XML from URL
public String getXmlFromUrl(String url) {
String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return xml;
}
//Get dom element
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
//Get nod element
public final String getElementValue(Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
//Get element value
public String getValue(Element item, String str) {
NodeList nlList = item.getElementsByTagName(str).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
}
Any ideas ? I seriously don't know what is wrong.. I used this before and it worked.
© Stack Overflow or respective owner