2012-09-26 65 views
1

我有一些問題,獲取從URL中的XML用下面的代碼XML:檢索URL

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:

公共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

請發表您的XML看起來像 – antoniom

+0

像這樣: http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key = b25b959554ed76058ac220b7b2e0a026&artist = Radiohead&album = In%20raynbows – Pl4za

+0

有什麼錯誤?你能調試和驗證字符串XML是否有效XML是有效的XML? – Billdr

回答

0

我拉你的代碼,並試用它。我redid「getAlbumURL」返回一個硬編碼的字符串。我用你在評論中提供的地址,它失敗了。但是,該XML不鏈接到任何專輯封面。所以我嘗試了正確的專輯名稱「In%20ra * i * nbows」。像夢一樣工作。所以首先,驗證你的常量給你你認爲他們應該做的(你已經做了),然後驗證你的測試數據。你的代碼看起來很好。

+0

我沒有使用我給你的示例URL進行測試,我只使用了一個單詞藝術家和專輯,而且它正在輸出正確的值。 我沒有改變任何東西,它現在正在工作..我只添加了一種方法來添加%20到多個單詞藝術家和專輯。 不知道我錯過了什麼。 感謝您的時間!對此,我真的非常感激 ! ;) – Pl4za

0

AFTE DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();try{}塊之前加上這些行:

dbf.setIgnoringComments(true); 
dbf.setCoalescing(true);