How to get data from API (JSON) in android?

I tried to get some data from song API. What I want to get is the tags from the song.

Here the JSON :

{"track":
    {"name":"Let It Happen",
    "url":"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen",
    "duration":"469000",
    "streamable":{"#text":"0","fulltrack":"0"},
    "listeners":"150111",
    "playcount":"758518",
    "artist":{"name":"Tame Impala","mbid":"63aa26c3-d59b-4da4-84ac-716b54f1ef4d","url":"http://www.last.fm/music/Tame+Impala"},
    "toptags":
        {"tag":[
            {"name":"indie","url":"http://www.last.fm/tag/indie"},
            {"name":"psychedelic","url":"http://www.last.fm/tag/psychedelic"},
            {"name":"Psychedelic Rock","url":"http://www.last.fm/tag/Psychedelic+Rock"},
            {"name":"2015","url":"http://www.last.fm/tag/2015"},
            {"name":"electronic","url":"http://www.last.fm/tag/electronic"}
            ]
        },
    "wiki":
        {"published":"11 Mar 2015, 08:59","summary":"Let It Happen is the opening track and first song revealed from Tame Impala's upcoming third album, Currents. <a href=\"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen\">Read more on Last.fm</a>.","content":"Let It Happen is the opening track and first song revealed from Tame Impala's upcoming third album, Currents. <a href=\"http://www.last.fm/music/Tame+Impala/_/Let+It+Happen\">Read more on Last.fm</a>. User-contributed text is available under the Creative Commons By-SA License; additional terms may apply."}
    }
}

From what I've learned, I use this code to parse it

private static ArrayList<String> parseTagsAPI(final String response)
{
    ArrayList<String> tagTemp = new ArrayList<String>();
    try
    {
        String tag="", logtag="test, test2";

        JSONObject jsonObject = new JSONObject(response);
        if (jsonObject.has("track"))
        {
            JSONObject jsonTrack = jsonObject.getJSONObject("track");
            Log.d("API (tag)", "track");

            if (jsonObject.has("toptags"))
            {
                JSONArray jsonArray = jsonTrack.getJSONObject("toptags").getJSONArray("tag");
                Log.d("API (tag)", "track > album > toptags > tag(array)");

                for (int i = 0; i < jsonArray.length(); i++)
                {
                    if (jsonArray.getJSONObject(i).has("name"))
                    {
                        JSONObject tagArray = jsonArray.getJSONObject(i).getJSONObject("name");
                        tag = tagArray.optString("name");

                        logtag += (tag + ", ");
                        Log.d("API (tag)", "tags : " + tag);
                    }
                    tagTemp.add(tag);
                }
            }
        }
        Log.d("API (tag)", "Logtag : "+logtag);
    } catch (Exception e) {
        e.printStackTrace();
        return new ArrayList<String>();
    }
    return tagTemp;
}

I put some logs to mark the progress, but it only show 2 logs:

which means my second if doesnt work. Anyone know why this is happen?

Upvotes: 0

Views: 8216

Answers (2)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

You're looking for the wrong tag - it's just a typo. 

    private static ArrayList<String> parseTagsAPI(final String response)
{   ArrayList<String> tagTemp = new ArrayList<String>();
    try
    {   String tag="", logtag="test, test2";

        JSONObject jsonObject = new JSONObject(response);
        if (jsonObject.has("track"))
        {   JSONObject jsonTrack = jsonObject.getJSONObject("track");
            Log.d("API (tag)", "track");

            if (jsonObject.has("toptags"))
            {   JSONArray jsonArray = jsonTrack.getJSONObject("toptags").getJSONArray("tag");
                Log.d("API (tag)", "track > album > toptags > tag(array)");

Notice that you're looking for jsonObject.has("toptags") rather than jsonTrack.has("toptags");

Upvotes: 1

Aakash
Aakash

Reputation: 5251

It is wrong here:

JSONObject tagArray = jsonArray.getJSONObject(i).getJSONObject("name");
tag = tagArray.optString("name");

you should do instead:

tag = jsonArray.getJSONObject(i).getString("name");

or

tag = jsonArray.getJSONObject(i).optString("name");

Also change this:

if (jsonObject.has("toptags"))

to

if (jsonTrack.has("toptags"))

Upvotes: 1

Related Questions