我正在尝试从Last.fm获取并应用艺术家图像到ImageVIEw,但没有返回任何图像.我不确定我在这里做错了什么.
private voID setLastFmArtistimage() { try { String imageurl = "http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=" + URLEncoder.encode("Andrew Bird") + "&API_key=" + APIKEY + "&limit=" + 1 + "&page=" + 1; inputStream in = null; Log.i("URL", imageurl); URL url = new URL(imageurl); URLConnection urlConn = url.openConnection(); httpURLConnection httpConn = (httpURLConnection) urlConn; httpConn.connect(); in = httpConn.getinputStream(); Bitmap bmpimg = BitmapFactory.decodeStream(in); mArtistBackground.setimageBitmap(bmpimg); } catch (MalformedURLException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); }}
解决方法:
您尝试使用的API会返回XML,而不是图像.您需要解析响应并从响应中选择适当的图像URL.
API documentation是非常彻底的,查看每个喜欢的艺术家Benny Hill的样本响应将为您提供足够的方向来找到合适的图像来显示.
编辑:有关API的示例,您可以查看官方Last.fm client – 请注意,这是GPL3授权的东西,除非您想要发布您的来源,否则您不应该使用副本&糊.
编辑(再次):对于未受GPL3污染的示例,请尝试以下 *** 作:
(该示例使用JSoup,友好的XML解析器)
public List<LastFmImage> getLastFmImages(String artistname, int limit, int page) throws IOException { String APIUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=" + URLEncoder.encode(artistname) + "&API_key=" + APIKEY + "&limit=" + limit + "&page=" + page; document doc = Jsoup.connect(APIUrl).timeout(20000).get(); Elements images = doc.select("images"); ArrayList<LastFmImage> result = new ArrayList<LastFmImage>(); final int nbrofImages = images.size(); for (int i = 0; i < nbrofImages; i++) { Element image = images.get(i); String Title = image.select("Title").first().text(); Elements sizes = image.select("sizes").select("size"); final int nbrofSizes = sizes.size(); for (int j = 0; j < nbrofSizes; j++) { Element size = sizes.get(i); result.add(new LastFmImage(Title, size.text(), size.attr("name"), Integer.parseInt(size.attr("wIDth")), Integer.parseInt(size.attr("height")))); } } return result;}
和LastFmImage类:
public class LastFmImage { public String mTitle; public String mUrl; public String mname; public int mWIDth; public int mHeight; public LastFmImage(String Title, String url, String name, int wIDth, int height) { mTitle = Title; mUrl = url; mname = name; mWIDth = wIDth; mHeight = height; }}
总结 以上是内存溢出为你收集整理的java – Last.fm不会返回艺术家图片全部内容,希望文章能够帮你解决java – Last.fm不会返回艺术家图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)