我在使用以下代码从 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;
}
获取相册网址:
public static String getAlbumURL(String artist, String album){
return URL_METHOD + METHOD_GETALBUM + AMPERSAND
+ API_KEY + AMPERSAND
+ PARAM_ARTIST + artist + AMPERSAND
+ PARAM_ALBUM + album;
}
XML解析器:
公共类 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();
}
}
有任何想法吗 ?我真的不知道出了什么问题..我以前使用过它并且它有效。