Search code examples
androidspotifylibspotify

Spotify Android SDK Searching for track number


I am currently connected to the Spotify sdk and I am currently looking to be able to have a user search for a track and it will return the track number. I have followed the sdk guides to track a song and I get the response as a JSON response, but when I try to create a JSONArray in android from the string response I get, it tells me that it is unable to create the JSONArray. The response I am currently getting is something like this

{
  "info": {
    "num_results": 601,
    "limit": 100,
    "offset": 0,
    "query": "what do you mean",
    "type": "track",
    "page": 1
  },
  "tracks": [
    {
      "album": {
        "released": "2015",
        "href": "spotify:album:7fZH0aUAjY3ay25obOUf2a",
        "name": "Purpose (Deluxe)",
        "availability": {
          "territories": "AD AR AT AU BE BG BO BR CH CL CO CR CY CZ DE DK DO EC EE ES FI FR GB GR GT HK HN HR HU IE IS IT LI LT LU LV MC MT NI NL NO NZ PA PE PH PL PT PY RO SE SI SK SV TR TW UY"
        }
      },
      "name": "What Do You Mean?",
      "popularity": "0.97",
      "external-ids": [
        {
          "type": "isrc",
          "id": "USUM71511919"
        }
      ],
      "length": 205.68,
      "href": "spotify:track:3pzjHKrQSvXGHQ98dx18HI",
      "artists": [
        {
          "href": "spotify:artist:1uNFoZAHBGtllmzznpCI3s",
          "name": "Justin Bieber"
        }
      ],
      "track-number": "3"
    },
    {
      "album": {
        "released": "2015",
        "href": "spotify:album:6Fr2rQkZ383FcMqFyT7yPr",
        "name": "Purpose (Deluxe)",
        "availability": {
          "territories": "CA MX US"
        }
      },
      "name": "What Do You Mean?",
      "popularity": "0.93",
      "external-ids": [
        {
          "type": "isrc",
          "id": "USUM71511919"
        }
      ],
      "length": 205.68,
      "href": "spotify:track:4B0JvthVoAAuygILe3n4Bs",
      "artists": [
        {
          "href": "spotify:artist:1uNFoZAHBGtllmzznpCI3s",
          "name": "Justin Bieber"
        }
      ],
      "track-number": "3"
    },
    {
      "album": {
        "released": "2015",
        "href": "spotify:album:2hL8vuRtlo75Wr9PyZI5Jb",
        "name": "What Do You Mean?",
        "availability": {
          "territories": ""
        }
      },
      "name": "What Do You Mean?",
      "popularity": "0.87",
      "external-ids": [
        {
          "type": "isrc",
          "id": "USUM71511919"
        }
      ],
      "length": 207.546,
      "href": "spotify:track:1ds2QsfhAAfRiaFMGDzrdb",
      "artists": [
        {
          "href": "spotify:artist:1uNFoZAHBGtllmzznpCI3s",
          "name": "Justin Bieber"
        }
      ],
      "track-number": "1"
    },
    {
      "album": {
        "released": "2015",
        "href": "spotify:album:7fZH0aUAjY3ay25obOUf2a",
        "name": "Purpose (Deluxe)",
        "availability": {
          "territories": "AD AR AT AU BE BG BO BR CH CL CO CR CY CZ DE DK DO EC EE ES FI FR GB GR GT HK HN HR HU IE IS IT LI LT LU LV MC MT NI NL NO NZ PA PE PH PL PT PY RO SE SI SK SV TR TW UY"
        }
      },
      "name": "What Do You Mean? - Acoustic",
      "popularity": "0.85",
      "external-ids": [
        {
          "type": "isrc",
          "id": "USUM71516855"
        }
      ],
      "length": 203.8,
      "href": "spotify:track:5bldrrpdHrTeaWNT1Kp5xs",
      "artists": [
        {
          "href": "spotify:artist:1uNFoZAHBGtllmzznpCI3s",
          "name": "Justin Bieber"
        }
      ],
      "track-number": "19"
    },
    {
      "album": {
        "released": "2015",
        "href": "spotify:album:6Fr2rQkZ383FcMqFyT7yPr",
        "name": "Purpose (Deluxe)",
        "availability": {
          "territories": "CA MX US"
        }

and my android code looks like this

 try {
            response = httpClient.execute(httpPost);
            responseBody = EntityUtils.toString(response.getEntity());
            try {
                jsonArray = new JSONArray(responseBody);
            } catch (JSONException e) {
                e.printStackTrace();
            }

Thank you for the help ahead of time ~Rockyfish


Solution

  • Do this

    JSONObject jObj;
    try {
            jObj = new JSONObject(responseBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }