-2

Here is what I have so far:

 public Bitmap getAlbumCover(Context context, String song, String artist) {
    this.context = context;
    song = song.replace(" ", "%20");
    artist = artist.replace(" ", "%20");

    try {
        conn = new URL("https://api.spotify.com/v1/search?q=track" + song + ":%20artist:" + artist + "&type=track)").openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (conn != null)
        conn.setDoOutput(true);


    try {
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (reader != null) {
        // Read Server Response
        String line2 = null;
        try {
            while ((line2 = reader.readLine()) != null) {
                sb.append(line2);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            json = new JSONArray(sb.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    JSONParser parser= new JSONParser();
    try {
        JSONObject jsonObject = (JSONObject) parser.parse(reader);
        try {
            array = (JSONArray) jsonObject.get("items");
        } catch (JSONException e) {
            e.printStackTrace();
        }


        // take each value from the json array separately

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;

}

The JSON I am using is located here: https://api.spotify.com/v1/search?q=track:Ready%20To%20Fall%20artist:rise%20against%20&type=track

I am trying to get the image url located in the images array and the preview_track url located in items.

Taylor Courtney
  • 813
  • 1
  • 6
  • 23
  • You already have `JSONObject jsonObject = (JSONObject) parser.parse(reader);` so this should answer your question, doesn't it? Just expand on that to extract the value you need. – Thomas Mar 23 '16 at 14:45
  • 2
    I would recommend Jackson databind for parsing JSON with Java - https://github.com/FasterXML/jackson-databind/ – Zack Macomber Mar 23 '16 at 14:45
  • Did you try searching? http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Michal Partyka Mar 23 '16 at 14:47

1 Answers1

1

I use Jackson library to parse JSON to java opbject.

if you create your java object with the same structure as JSON this can be done using this:

   ObjectMapper mapper = new ObjectMapper();
   mapper.readValue(jsonUrl, YourClass.class);

So your OBJECT will have tracks and then tracks will have object album and album will have object other details. Just structure it as the JSON is and you are there.

Ankit
  • 257
  • 1
  • 3
  • 13