Mes Mes - 4 months ago 8
Android Question

Gson deserialisation result is null for one type

I'm getting a response which consists of two types of objects :

Pagination
and a List of
ArtistSearch
. The model I'm using for the deserialization is :

public class ArtistSearchResults {
List<ArtistSearch> artistSearchList;
Pagination pagination;

// getters and setters...

}


And I deserialize here :

Gson gson = new Gson();
ArtistSearchResults results = gson.fromJson(response.toString(), ArtistSearchResults.class);
List<ArtistSearch> artistSearchList = results.getArtistSearchList();
Pagination pagination = results.getPagination();

Log.i(TAG, "onSuccess Pagination size == " + pagination.getItems() );
Log.i(TAG, "onSuccess RESULTS size == " + artistSearchList.get(0).getTitle() );


and although I get correctly the response for the
Pagination
object I'm always getting null for the artistSearchList.

I'm not getting any error messages so I'm not sure where the error is.
The kind of results I'm getting looks like this :

07-10 06:11:06.726 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Search-Artist response = {"pagination":{"per_page":5,"pages":3,"page":1,"urls":{"last":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=3","next":"https:\/\/api.discogs.com\/database\/search?q=savage+republic&per_page=5&type=artist&page=2"},"items":14},"results": [{"thumb":"https:\/\/api-img.discogs.com\/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-121133-1262786660.jpeg.jpg","title":"Savage Republic","uri":"\/artist\/121133-Savage-Republic","resource_url":"https:\/\/api.discogs.com\/artists\/121133","type":"artist","id":121133},{"thumb":"https:\/\/api-img.discogs.com\/GYkPyAYZEcFM0bzyjnBple7P3Yw=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-307086-1160815350.jpeg.jpg","title":"Bruce Licher","uri":"\/artist\/307086-Bruce-Licher","resource_url":"https:\/\/api.discogs.com\/artists\/307086","type":"artist","id":307086},{"thumb":"https:\/\/api-img.discogs.com\/XHvbh885CZ2uTcq7WT3ph0n0gMs=\/150x150\/smart\/filters:strip_icc():format(jpeg):mode_rgb():quality(40)\/discogs-images\/A-71224-1334785233.jpeg.jpg","title":"Medicine (2)","uri":"\/artist\/71224-Medicine-2","resource_url":"https:\/\/api.discogs.com\/artists\/71224","type":"artist","id":71224},{"thumb":"","title":"Val Haller","uri":"\/artist\/492425-Val-Haller","resource_url":"https:\/\/api.discogs.com\/artists\/492425","type":"artist","id":492425},{"thumb":"","title":"Ramona Clarke","uri":"\/artist\/1957827-Ramona-Clarke","resource_url":"https:\/\/api.discogs.com\/artists\/1957827","type":"artist","id":1957827}]}
07-10 06:11:06.732 6954-6954/jb.ti.discogsball I/SEARCHHANDLER: onSuccess Pagination size == 14


and as you can see I get a value for the
Pagination
object but I get a NPE for the artistSearchList.

The model I'm using for ArtistSearch is :

public class ArtistSearch {
int id;
String title;
String type;
String resource_url;
String uri;
String thumb;

// getters and setters ...


This is the text from the response that parses to null :


"results":[{"thumb":"https://api-img.discogs.com/RAHE1vqHWb1xwPUv5y2Q1v5g4Yo=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-121133-1262786660.jpeg.jpg","title":"Savage
Republic","uri":"/artist/121133-Savage-Republic","resource_url":"https://api.discogs.com/artists/121133","type":"artist","id":121133},{"thumb":"https://api-img.discogs.com/GYkPyAYZEcFM0bzyjnBple7P3Yw=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-307086-1160815350.jpeg.jpg","title":"Bruce
Licher","uri":"/artist/307086-Bruce-Licher","resource_url":"https://api.discogs.com/artists/307086","type":"artist","id":307086},{"thumb":"https://api-img.discogs.com/XHvbh885CZ2uTcq7WT3ph0n0gMs=/150x150/smart/filters:strip_icc():format(jpeg):mode_rgb():quality(40)/discogs-images/A-71224-1334785233.jpeg.jpg","title":"Medicine
(2)","uri":"/artist/71224-Medicine-2","resource_url":"https://api.discogs.com/artists/71224","type":"artist","id":71224},{"thumb":"","title":"Val
Haller","uri":"/artist/492425-Val-Haller","resource_url":"https://api.discogs.com/artists/492425","type":"artist","id":492425},{"thumb":"","title":"Ramona
Clarke","uri":"/artist/1957827-Ramona-Clarke","resource_url":"https://api.discogs.com/artists/1957827","type":"artist","id":1957827}]

Answer

Could you please replace the following variable declaration in ArtistSearchResults.java:

private List<ArtistSearch> artistSearchList;

by this:

@SerializedName("results")
private List<ArtistSearch> artistSearchList;

and see the results?

Comments