4

谁能告诉我是否有任何简单的 Web API 可以将专辑封面作为图像返回,就像 Google Static Map API(Google Static Map API一样?我听说过 Amazon 和 Last.fm 的 API,但找不到任何代码(除了 PHP/Perl/Ruby 等 Web 语言)。此外,它们是 REST API。任何帮助将不胜感激。

是否有简单的 URL(GET) 查询或只有 REST/XML 方法?

4

2 回答 2

3

It shouldn't be very hard to do by hand. The last.fm api spec for Album.getInfo method is pretty straightforward and the fact that it is a REST api makes it even simpler. Just get the xml response from the server, parse it and get a url of the image. I wrote this code a while ago but it should still be working:

            String request = "http://ws.audioscrobbler.com/2.0/?method=" + method +
                    "&api_key="+apiKey;
            request += "&artist=" + artist.replaceAll(" ", "%20");
            if (method.equals("album.getinfo")) request += "&album=" + album.replaceAll(" ", "%20");
            URL url = new URL(request);
            InputStream is = url.openStream();
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = db.parse(is);

            NodeList nl = doc.getElementsByTagName("image");
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n.getAttributes().item(0).getNodeValue().equals(imageSize)) {
                    Node fc = n.getFirstChild();
                    if (fc == null) return null;
                    String imgUrl = fc.getNodeValue();
                    if (imgUrl.trim().length() == 0) return null;
                    return new ImageIcon(new URL(imgUrl));
                }
            }
于 2010-12-21T15:42:02.597 回答
0

这是 C# SOAP/REST 的 Amazon API 示例

于 2010-12-21T15:29:29.827 回答