0
votes

I'm using the spotify web api to get a json object containing album information.

Here is the model url

https://api.spotify.com/v1/search?q=album:arrival%20artist:abba&type=album

So let's say I'm looking for the album "Nevermind" by "Nirvana" and I have this url

https://api.spotify.com/v1/search?q=album:nevermind%20artist:nirvana&type=album

So I've made a "model object" for Gson here

public class SpotifyAlbumInfo {

    public CollectionInfo collectionInfo;

    public class CollectionInfo {

        String href;
        List<Album> albums;     
        Integer limit;
        String next;      
        Integer offset;
        String previous;  
        Integer total6;

        public class Album{
            String album_type;
            String[] available_markets;
            Urls external_urls;
            String href;
            String id;
            List<Image> images;
            String name;
            String type;
            String uri;

            public class Urls{
                String source;
            }

            public class Image {
                Integer height;
                String url;
                Integer width;
            }
        }
    }
}

And here I'm trying to get the first image url

for(int i=0;i<response.collectionInfo.albums.images.size();i++){       
    if(response.collectionInfo.albums.image.get(i).contains("nevermind")){
        imageUrl = response.collectionInfo.albums.images.get(i).url;
        break;
    }
}

The problem is that this statement is fine

response.collectionInfo.albums.size();

But I can't use

response.collectionInfo.albums.images.size();

Because I get the error symbol does not exist.

I get the same error for these statements.

response.collectionInfo.albums.images.size(); 
response.collectionInfo.albums.album_type.size();
response.collectionInfo.albums.href.size();
// etc

How do I go "a layer deeper" into my "model object" and get the image url I want?

1
Why are you using the web api instead of the Android SDK?OneCricketeer
I didn't know I could use the SDK to do this.the_prole
Neither do I, but it's worth a look rather than dealing with manually parsing JSON responsesOneCricketeer

1 Answers

2
votes

You need to get a specific album from the list

collectionInfo.albums

There is no images field for a List interface, which some error code is probably trying to tell you.

You can drill down like so

Album album0 = collectionInfo.albums.get(0);
album0.images.size()