我在使用以下代码从url检索xml时遇到一些问题:
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;
}
我在上面的代码中调用的xml函数:
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();
}
}
有什么想法吗?我真的不知道出了什么问题。我之前使用过它并且有效。
答案 0 :(得分:0)
我拉了你的代码并试了一下。我重新编写了“getAlbumURL”来返回一个硬编码的字符串。我使用了您在评论中提供的地址,但失败了。但是,该xml不会链接到任何专辑封面。所以我尝试了正确的专辑名称“In%20ra * i * nbows”。像梦一样工作。
首先,验证您的常量是否为您提供了您认为应该使用的内容(您已经做过),然后验证您的测试数据。你的代码看起来很好。
答案 1 :(得分:0)
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
在try{}
块之后添加这些行:
dbf.setIgnoringComments(true);
dbf.setCoalescing(true);