1

我在使用以下代码从 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();
    }

}

有任何想法吗 ?我真的不知道出了什么问题..我以前使用过它并且它有效。

4

2 回答 2

0

在块之前DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();添加这些行之后:try{}

dbf.setIgnoringComments(true);
dbf.setCoalescing(true); 
于 2013-02-16T16:35:22.017 回答
0

我提取了您的代码并进行了尝试。我重新编写了“getAlbumURL”以返回一个硬编码的字符串。我使用了您在评论中提供的地址,它失败了。但是,该 xml 没有链接到任何专辑封面。所以我尝试了正确的专辑名称,“In%20ra*i*nbows”。像梦一样工作。

因此,首先,验证您的常量是否为您提供了您认为它们应该提供的内容(您已经这样做了),其次,验证您的测试数据。您的代码似乎很好。

于 2012-09-28T21:35:15.483 回答