提出要求

时间:2015-01-20 16:22:49

标签: java spotify

我需要帮助从spotify获取信息。如何从此链接:https://api.spotify.com/v1/search?q=Songs+of+Innocence&type=album 我可以拿网址:

"height" : 64,
        "url" : "https://i.scdn.co/image/eb740cef5aa3d5e119baf868bdff2dbb5cc1a59b",
        "width" : 64

在我的代码中将url分配给变量s:

  public void getCover(String album) {

            String query = "https://api.spotify.com/v1/search?q="+ encodeField(album)+"=album";
            java.net.URL url = null;
            try {
                BufferedImage image = null;
                url = new java.net.URL(query);
            } catch (MalformedURLException e1) {            
                e1.printStackTrace();
            }
            InputStream is = null;
            try {
                is = url.openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // get the text from the stream as lines
            java.io.BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String s;
            try {
                s = reader.readLine()) // read link with image 64x64 resolution
            } catch (IOException e) {

                e.printStackTrace();
            }
            try {
                //URL url2 = new URL("https://i.scdn.co/image/eb740cef5aa3d5e119baf868bdff2dbb5cc1a59b");
                URL url2 = new URL(s);
                image = ImageIO.read(url2);
                ImageIcon icon = new ImageIcon(image);
                jLabel2.setIcon(icon);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

1 个答案:

答案 0 :(得分:0)

有许多库可以帮助您解析Java中的json。例如,请参阅:How to parse JSON in Java